瀏覽代碼

Replace strdup plus error checking usage with a new m_strdup function

Closes: #379028
Guillem Jover 18 年之前
父節點
當前提交
7671a7a35a
共有 11 個文件被更改,包括 50 次插入29 次删除
  1. 14 0
      ChangeLog
  2. 3 1
      debian/changelog
  3. 1 1
      dpkg-deb/build.c
  4. 1 3
      lib/database.c
  5. 1 0
      lib/dpkg.h
  6. 14 0
      lib/mlib.c
  7. 2 1
      lib/myopt.c
  8. 3 11
      lib/tarfn.c
  9. 2 2
      src/configure.c
  10. 2 1
      src/help.c
  11. 7 9
      src/main.c

+ 14 - 0
ChangeLog

@@ -1,3 +1,17 @@
+2008-01-21  Guillem Jover  <guillem@debian.org>
+
+	* lib/mlib.c (m_strdup): New function.
+	* lib/dpkg.h (m_strdup): New declaration.
+	* lib/tarfn.c: Include <dpkg.h>.
+	(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  <guillem@debian.org>
 
 	* configure.ac: Bump version to 1.14.17~.

+ 3 - 1
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 <guillem@debian.org>  Mon, 21 Jan 2008 10:11:55 +0200
 

+ 1 - 1
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;
 }

+ 1 - 3
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++; }
   

+ 1 - 0
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]);

+ 14 - 0
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);
 }

+ 2 - 1
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);

+ 3 - 11
lib/tarfn.c

@@ -14,6 +14,7 @@
 #include <grp.h>
 #include <errno.h>
 #include <tarfn.h>
+#include <dpkg.h>
 
 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);

+ 2 - 2
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("-");
 	}
 }
 

+ 2 - 1
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*);

+ 7 - 9
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<argc;i++)
-	if (newargs[i]) newargs[i]=strdup(newargs[i]);
+      if (newargs[i])
+        newargs[i] = m_strdup(newargs[i]);
 
     cipaction= NULL;
     myopt((const char *const**)&newargs,cmdinfos);