From 9ba4f3d00623bf0256e4a363444823519f9fb8b1 Mon Sep 17 00:00:00 2001 From: kaiw Date: Sun, 24 Feb 2008 17:57:29 +0000 Subject: Handle properly when insert zero size objects into the archive: Do not mmap 0-size objects and do not try to extract symbol from 0-size objects, but do treat 0-size objects as qualified objects and accept them as an archive member. (A member with only the header part) Note that GNU binutils ar on FreeBSD ignores 0-size objects, but on Linux it accepts them. [1] But, since this is a rare usage, we can safely ignore the compatibility issue. Reported by: Michael Plass Pointed out by: Michael Plass [1] Reviewed by: Michael Plass Reviewed by: jkoshy Approved by: jkoshy (mentor) --- usr.bin/ar/write.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/usr.bin/ar/write.c b/usr.bin/ar/write.c index 959fb68..2ead060 100644 --- a/usr.bin/ar/write.c +++ b/usr.bin/ar/write.c @@ -154,6 +154,12 @@ create_obj_from_file(struct bsdar *bsdar, const char *name, time_t mtime) obj->mtime = sb.st_mtime; obj->dev = sb.st_dev; obj->ino = sb.st_ino; + + if (obj->size == 0) { + obj->maddr = NULL; + return(obj); + } + if ((obj->maddr = mmap(NULL, obj->size, PROT_READ, MAP_PRIVATE, obj->fd, (off_t)0)) == MAP_FAILED) { bsdar_warnc(bsdar, errno, "can't mmap file: %s", obj->name); @@ -427,14 +433,14 @@ write_cleanup(struct bsdar *bsdar) struct ar_obj *obj, *obj_temp; TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) { - free(obj->name); if (obj->fd == -1) free(obj->maddr); else - if (munmap(obj->maddr, obj->size)) + if (obj->maddr != NULL && munmap(obj->maddr, obj->size)) bsdar_warnc(bsdar, errno, "can't munmap file: %s", obj->name); TAILQ_REMOVE(&bsdar->v_obj, obj, objs); + free(obj->name); free(obj); } @@ -478,7 +484,7 @@ write_objs(struct bsdar *bsdar) /* Create archive symbol table and archive string table, if need. */ TAILQ_FOREACH(obj, &bsdar->v_obj, objs) { - if (!(bsdar->options & AR_SS)) + if (!(bsdar->options & AR_SS) && obj->maddr != NULL) create_symtab_entry(bsdar, obj->maddr, obj->size); if (strlen(obj->name) > _MAXNAMELEN_SVR4) add_to_ar_str_table(bsdar, obj->name); -- cgit v1.1