Просмотр исходного кода

Use m_asprintf() instead of m_malloc() and string operations

This avoid the need to know the resulting string length beforehand, and
makes sure it's always going to get the right size. It also makes the
code way clearer.
Guillem Jover лет назад: 15
Родитель
Сommit
6185545a79
7 измененных файлов с 20 добавлено и 46 удалено
  1. 2 7
      lib/dpkg/dbmodify.c
  2. 2 4
      lib/dpkg/dump.c
  3. 7 16
      lib/dpkg/myopt.c
  4. 2 4
      lib/dpkg/tarfn.c
  5. 2 3
      src/archives.c
  6. 3 7
      src/main.c
  7. 2 5
      src/processarc.c

+ 2 - 7
lib/dpkg/dbmodify.c

@@ -175,13 +175,9 @@ modstatdb_is_locked(const char *admindir)
 void
 modstatdb_lock(const char *admindir)
 {
-  int n;
   char *dblockfile = NULL;
 
-  n = strlen(admindir);
-  dblockfile = m_malloc(n + sizeof(LOCKFILE) + 2);
-  strcpy(dblockfile, admindir);
-  strcpy(dblockfile + n, "/" LOCKFILE);
+  m_asprintf(&dblockfile, "%s/%s", admindir, LOCKFILE);
 
   if (dblockfd == -1) {
     dblockfd = open(dblockfile, O_RDWR | O_CREAT | O_TRUNC, 0660);
@@ -210,8 +206,7 @@ modstatdb_init(const char *admindir, enum modstatdb_rw readwritereq)
 
   for (fnip=fnis; fnip->suffix; fnip++) {
     free(*fnip->store);
-    *fnip->store = m_malloc(strlen(admindir) + strlen(fnip->suffix) + 2);
-    sprintf(*fnip->store, "%s/%s", admindir, fnip->suffix);
+    m_asprintf(fnip->store, "%s/%s", admindir, fnip->suffix);
   }
 
   cflags= readwritereq & msdbrw_flagsmask;

+ 2 - 4
lib/dpkg/dump.c

@@ -386,10 +386,8 @@ writedb(const char *filename, bool available, bool mustsync)
   int old_umask;
 
   which = available ? "available" : "status";
-  oldfn= m_malloc(strlen(filename)+sizeof(OLDDBEXT));
-  strcpy(oldfn,filename); strcat(oldfn,OLDDBEXT);
-  newfn= m_malloc(strlen(filename)+sizeof(NEWDBEXT));
-  strcpy(newfn,filename); strcat(newfn,NEWDBEXT);
+  m_asprintf(&oldfn, "%s%s", filename, OLDDBEXT);
+  m_asprintf(&newfn, "%s%s", filename, NEWDBEXT);
 
   old_umask = umask(022);
   file= fopen(newfn,"w");

+ 7 - 16
lib/dpkg/myopt.c

@@ -156,8 +156,7 @@ load_config_dir(const char *prog, const struct cmdinfo* cmdinfos)
   struct dirent **dlist;
   int dlist_n, i;
 
-  dirname = m_malloc(strlen(CONFIGDIR "/.cfg.d") + strlen(prog) + 1);
-  sprintf(dirname, "%s/%s.cfg.d", CONFIGDIR, prog);
+  m_asprintf(&dirname, "%s/%s.cfg.d", CONFIGDIR, prog);
 
   dlist_n = scandir(dirname, &dlist, valid_config_filename, alphasort);
   if (dlist_n < 0) {
@@ -171,9 +170,7 @@ load_config_dir(const char *prog, const struct cmdinfo* cmdinfos)
   for (i = 0; i < dlist_n; i++) {
     char *filename;
 
-    filename = m_malloc(strlen(dirname) + 1 + strlen(dlist[i]->d_name) + 1);
-    sprintf(filename, "%s/%s", dirname, dlist[i]->d_name);
-
+    m_asprintf(&filename, "%s/%s", dirname, dlist[i]->d_name);
     myfileopt(filename, cmdinfos);
 
     free(dlist[i]);
@@ -186,24 +183,18 @@ load_config_dir(const char *prog, const struct cmdinfo* cmdinfos)
 
 void loadcfgfile(const char *prog, const struct cmdinfo* cmdinfos) {
   char *home, *file;
-  int l1, l2;
 
   load_config_dir(prog, cmdinfos);
 
-  l1 = strlen(CONFIGDIR "/.cfg") + strlen(prog);
-  file = m_malloc(l1 + 1);
-  sprintf(file, CONFIGDIR "/%s.cfg", prog);
+  m_asprintf(&file, "%s/%s.cfg", CONFIGDIR, prog);
   myfileopt(file, cmdinfos);
+  free(file);
+
   if ((home = getenv("HOME")) != NULL) {
-    l2 = strlen(home) + 1 + strlen("/.cfg") + strlen(prog);
-    if (l2 > l1) {
-      free(file);
-      file = m_malloc(l2 + 1);
-    }
-    sprintf(file, "%s/.%s.cfg", home, prog);
+    m_asprintf(&file, "%s/.%s.cfg", home, prog);
     myfileopt(file, cmdinfos);
+    free(file);
   }
-  free(file);
 }
 
 void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos) {

+ 2 - 4
lib/dpkg/tarfn.c

@@ -100,10 +100,8 @@ get_prefix_name(struct tar_header *h)
 {
 	char *path;
 
-	path = m_malloc(sizeof(h->prefix) + 1 + sizeof(h->name) + 1);
-
-	sprintf(path, "%.*s/%.*s", (int)sizeof(h->prefix), h->prefix,
-	        (int)sizeof(h->name), h->name);
+	m_asprintf(&path, "%.*s/%.*s", (int)sizeof(h->prefix), h->prefix,
+	           (int)sizeof(h->name), h->name);
 
 	return path;
 }

+ 2 - 3
src/archives.c

@@ -1222,9 +1222,8 @@ void archivefiles(const char *const *argv) {
       for (ap = argv; *ap; ap++) {
         if (strchr(FIND_EXPRSTARTCHARS,(*ap)[0])) {
           char *a;
-          a= m_malloc(strlen(*ap)+10);
-          strcpy(a,"./");
-          strcat(a,*ap);
+
+          m_asprintf(&a, "./%s", *ap);
           command_add_arg(&cmd, a);
         } else {
           command_add_arg(&cmd, (const char *)*ap);

+ 3 - 7
src/main.c

@@ -264,9 +264,7 @@ setfilter(const struct cmdinfo *cip, const char *value)
 static void setroot(const struct cmdinfo *cip, const char *value) {
   char *p;
   instdir= value;
-  p= m_malloc(strlen(value) + sizeof(ADMINDIR));
-  strcpy(p,value);
-  strcat(p,ADMINDIR);
+  m_asprintf(&p, "%s%s", value, ADMINDIR);
   admindir= p;
 }
 
@@ -575,13 +573,11 @@ void execbackend(const char *const *argv) {
    * along to it. */
   if (strcmp(cipaction->parg, DPKGQUERY) == 0 &&
       strcmp(admindir, ADMINDIR) != 0) {
-    arg = m_malloc((strlen("--admindir=") + strlen(admindir) + 1));
-    sprintf(arg, "--admindir=%s", admindir);
+    m_asprintf(&arg, "--admindir=%s", admindir);
     command_add_arg(&cmd, arg);
   }
 
-  arg = m_malloc(2 + strlen(cipaction->olong) + 1);
-  sprintf(arg, "--%s", cipaction->olong);
+  m_asprintf(&arg, "--%s", cipaction->olong);
   command_add_arg(&cmd, arg);
 
   /* Exlicitely separate arguments from options as any user-supplied

+ 2 - 5
src/processarc.c

@@ -134,11 +134,8 @@ void process_archive(const char *filename) {
     int status;
 
     /* We can't ‘tentatively-reassemble’ packages. */
-    if (!reasmbuf) {
-      reasmbuf= m_malloc(admindirlen+sizeof(REASSEMBLETMP)+5);
-      strcpy(reasmbuf,admindir);
-      strcat(reasmbuf,"/" REASSEMBLETMP);
-    }
+    if (!reasmbuf)
+      m_asprintf(&reasmbuf, "%s/%s", admindir, REASSEMBLETMP);
     if (unlink(reasmbuf) && errno != ENOENT)
       ohshite(_("error ensuring `%.250s' doesn't exist"),reasmbuf);
     push_cleanup(cu_pathname, ~0, NULL, 0, 1, (void *)reasmbuf);