From 7671a7a35a6446495d4cf5255628ddb547c4885f Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Mon, 21 Jan 2008 10:30:33 +0200 Subject: [PATCH] Replace strdup plus error checking usage with a new m_strdup function Closes: #379028 --- ChangeLog | 14 ++++++++++++++ debian/changelog | 4 +++- dpkg-deb/build.c | 2 +- lib/database.c | 4 +--- lib/dpkg.h | 1 + lib/mlib.c | 14 ++++++++++++++ lib/myopt.c | 3 ++- lib/tarfn.c | 14 +++----------- src/configure.c | 4 ++-- src/help.c | 3 ++- src/main.c | 16 +++++++--------- 11 files changed, 50 insertions(+), 29 deletions(-) diff --git a/ChangeLog b/ChangeLog index f8bdccd8..0bd6ad65 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2008-01-21 Guillem Jover + + * lib/mlib.c (m_strdup): New function. + * lib/dpkg.h (m_strdup): New declaration. + * lib/tarfn.c: Include . + (TarExtractor): Use m_strdup instead of strdup. + * lib/database.c (findpackage): Likewise. + * lib/myopt.c (myfileopt): Likewise. + * dpkg-deb/build.c (getfi): Likewise. + * src/configure.c (md5hash): Likewise. + * src/help.c (vbuildarglist): Likewis. + * src/main.c (execbackend): Likewise. + (commandfd): Likewise. + 2008-01-21 Guillem Jover * configure.ac: Bump version to 1.14.17~. diff --git a/debian/changelog b/debian/changelog index 401ee04d..fa4a629e 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,6 +1,8 @@ dpkg (1.14.17) UNRELEASED; urgency=low - * + [ Guillem Jover ] + * Replace strdup plus error checking usage with a new m_strdup function. + Closes: #379028 -- Guillem Jover Mon, 21 Jan 2008 10:11:55 +0200 diff --git a/dpkg-deb/build.c b/dpkg-deb/build.c index 1046c7ef..a38109c3 100644 --- a/dpkg-deb/build.c +++ b/dpkg-deb/build.c @@ -133,7 +133,7 @@ static struct _finfo* getfi(const char* root, int fd) { fi=(struct _finfo*)malloc(sizeof(struct _finfo)); lstat(fn, &(fi->st)); - fi->fn=strdup(fn+rl+1); + fi->fn = m_strdup(fn + rl + 1); fi->next=NULL; return fi; } diff --git a/lib/database.c b/lib/database.c index 49013b7c..398af9ee 100644 --- a/lib/database.c +++ b/lib/database.c @@ -118,10 +118,8 @@ int informative(struct pkginfo *pkg, struct pkginfoperfile *info) { struct pkginfo *findpackage(const char *inname) { struct pkginfo **pointerp, *newpkg; - char *name = strdup(inname), *p; + char *name = m_strdup(inname), *p; - if (name == NULL) - ohshite(_("couldn't allocate memory for strdup in findpackage(%s)"),inname); p= name; while(*p) { *p= tolower(*p); p++; } diff --git a/lib/dpkg.h b/lib/dpkg.h index d49fdbd1..8d36fc88 100644 --- a/lib/dpkg.h +++ b/lib/dpkg.h @@ -222,6 +222,7 @@ void warningf(const char *fmt, ...) PRINTFFORMAT(1, 2); void setcloexec(int fd, const char* fn); void *m_malloc(size_t); void *m_realloc(void*, size_t); +char *m_strdup(const char *str); int m_fork(void); void m_dup2(int oldfd, int newfd); void m_pipe(int fds[2]); diff --git a/lib/mlib.c b/lib/mlib.c index 2842d881..da9b1dd8 100644 --- a/lib/mlib.c +++ b/lib/mlib.c @@ -67,6 +67,20 @@ void *m_realloc(void *r, size_t amount) { return r; } +char * +m_strdup(const char *str) +{ + char *new_str; + + onerr_abort++; + new_str = strdup(str); + if (!new_str) + ohshite(_("failed to allocate memory")); + onerr_abort--; + + return new_str; +} + static void print_error_forked(const char *emsg, const char *contextstring) { fprintf(stderr, _("%s (subprocess): %s\n"), thisname, emsg); } diff --git a/lib/myopt.c b/lib/myopt.c index 5aed2f33..84980a74 100644 --- a/lib/myopt.c +++ b/lib/myopt.c @@ -75,7 +75,8 @@ void myfileopt(const char* fn, const struct cmdinfo* cmdinfos) { if (cip->takesvalue) { if (!opt) ohshite(_("configuration error: %s needs a value"), linebuf); if (cip->call) cip->call(cip,opt); - else *cip->sassignto= strdup(opt); + else + *cip->sassignto = m_strdup(opt); } else { if (opt) ohshite(_("configuration error: %s does not take a value"), linebuf); if (cip->call) cip->call(cip,NULL); diff --git a/lib/tarfn.c b/lib/tarfn.c index e498da7d..3325d50e 100644 --- a/lib/tarfn.c +++ b/lib/tarfn.c @@ -14,6 +14,7 @@ #include #include #include +#include struct TarHeader { char Name[100]; @@ -195,17 +196,8 @@ TarExtractor( break; case SymbolicLink: memcpy(&symListBottom->h, &h, sizeof(TarInfo)); - if ((symListBottom->h.Name = strdup(h.Name)) == NULL) { - status = -1; - errno = 0; - break; - } - if ((symListBottom->h.LinkName = strdup(h.LinkName)) == NULL) { - free(symListBottom->h.Name); - status = -1; - errno = 0; - break; - } + symListBottom->h.Name = m_strdup(h.Name); + symListBottom->h.LinkName = m_strdup(h.LinkName); if ((symListBottom->next = malloc(sizeof(symlinkList))) == NULL) { free(symListBottom->h.LinkName); free(symListBottom->h.Name); diff --git a/src/configure.c b/src/configure.c index 800054f8..9c73f8dc 100644 --- a/src/configure.c +++ b/src/configure.c @@ -410,11 +410,11 @@ static void md5hash(struct pkginfo *pkg, char **hashbuf, const char *fn) { pop_cleanup(ehflag_normaltidy); /* fd= open(cdr.buf) */ close(fd); } else if (errno==ENOENT) { - *hashbuf= strdup(NONEXISTENTFLAG); + *hashbuf = m_strdup(NONEXISTENTFLAG); } else { fprintf(stderr, _("dpkg: %s: warning - unable to open conffile %s for hash: %s\n"), pkg->name, fn, strerror(errno)); - *hashbuf= strdup("-"); + *hashbuf = m_strdup("-"); } } diff --git a/src/help.c b/src/help.c index b6401ab9..3b9bc76c 100644 --- a/src/help.c +++ b/src/help.c @@ -204,7 +204,8 @@ static char *const *vbuildarglist(const char *scriptname, va_list ap) { i=0; if(bufs[0]) free(bufs[0]); - bufs[i++]= strdup(scriptname); /* yes, cast away const because exec wants it that way */ + /* Yes, cast away const because exec wants it that way */ + bufs[i++] = m_strdup(scriptname); for (;;) { assert(i < PKGSCRIPTMAXARGS); nextarg= va_arg(ap,char*); diff --git a/src/main.c b/src/main.c index 4f2f3d05..9ffc1613 100644 --- a/src/main.c +++ b/src/main.c @@ -493,9 +493,8 @@ void execbackend(const char *const *argv) { if (!nargv) ohshite(_("couldn't malloc in execbackend")); - nargv[i] = strdup(cipaction->parg); - if (!nargv[i]) - ohshite(_("couldn't strdup in execbackend")); + nargv[i] = m_strdup(cipaction->parg); + i++, offset++; if (pass_admindir) { @@ -515,11 +514,9 @@ void execbackend(const char *const *argv) { i++, offset++; /* Copy arguments from argv to nargv. */ - for (; i <= argc; i++) { - nargv[i] = strdup(argv[i - offset]); - if (!nargv[i]) - ohshite(_("couldn't strdup in execbackend")); - } + for (; i <= argc; i++) + nargv[i] = m_strdup(argv[i - offset]); + nargv[i] = NULL; execvp(cipaction->parg, nargv); @@ -609,7 +606,8 @@ printf("line=`%*s'\n",(int)linevb.used,linevb.buf); * least. */ for(i=1;i