installers: handle asprintf() correctly
authorH. Peter Anvin <hpa@zytor.com>
Fri, 2 Jul 2010 16:39:18 +0000 (09:39 -0700)
committerH. Peter Anvin <hpa@zytor.com>
Fri, 2 Jul 2010 16:39:18 +0000 (09:39 -0700)
It appears that the glibc version of asprintf() is braindamaged, and
doesn't set the target pointer to NULL in the event of an error (only
returns -1).  Therefore we need to check the return value.  Just in
case someone else made the *opposite* error, also check the pointer.

Bleh.  The glibc documentation states that *BSD sets the pointer to
NULL, but instead of following that, the glibc people put
warn_unused_result on asprintf.  Sigh.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
extlinux/main.c
libinstaller/advio.c
linux/syslinux.c

index 68e6457..daebc10 100644 (file)
@@ -350,12 +350,13 @@ int ext2_fat_install_file(const char *path, int devfd, struct stat *rst)
     char *file, *oldfile;
     int fd = -1, dirfd = -1;
     int modbytes;
+    int r1, r2;
 
-    asprintf(&file, "%s%sldlinux.sys",
-            path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/");
-    asprintf(&oldfile, "%s%sextlinux.sys",
-            path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/");
-    if (!file || !oldfile) {
+    r1 = asprintf(&file, "%s%sldlinux.sys",
+                 path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/");
+    r2 = asprintf(&oldfile, "%s%sextlinux.sys",
+                 path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/");
+    if (r1 < 0 || !file || r2 < 0 || !oldfile) {
        perror(program);
        return 1;
     }
index 7bfc098..56f607d 100644 (file)
@@ -45,11 +45,12 @@ int read_adv(const char *path, const char *cfg)
     int fd = -1;
     struct stat st;
     int err = 0;
+    int rv;
 
-    asprintf(&file, "%s%s%s",
-            path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/", cfg);
+    rv = asprintf(&file, "%s%s%s", path,
+                 path[0] && path[strlen(path) - 1] == '/' ? "" : "/", cfg);
 
-    if (!file) {
+    if (rv < 0 || !file) {
        perror(program);
        return -1;
     }
@@ -97,11 +98,12 @@ int write_adv(const char *path, const char *cfg)
     int fd = -1;
     struct stat st, xst;
     int err = 0;
+    int rv;
 
-    err = asprintf(&file, "%s%s%s",
-       path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/", cfg);
+    rv = asprintf(&file, "%s%s%s", path,
+                 path[0] && path[strlen(path) - 1] == '/' ? "" : "/", cfg);
 
-    if (!file) {
+    if (rv < 0 || !file) {
        perror(program);
        return -1;
     }
index 70fadcd..9462138 100644 (file)
@@ -261,11 +261,11 @@ int main(int argc, char *argv[])
     /* Note: subdir is guaranteed to start and end in / */
     if (opt.directory && opt.directory[0]) {
        int len = strlen(opt.directory);
-       asprintf(&subdir, "%s%s%s",
-                opt.directory[0] == '/' ? "" : "/",
-                opt.directory,
-                opt.directory[len-1] == '/' ? "" : "/");
-       if (!subdir) {
+       int rv = asprintf(&subdir, "%s%s%s",
+                         opt.directory[0] == '/' ? "" : "/",
+                         opt.directory,
+                         opt.directory[len-1] == '/' ? "" : "/");
+       if (rv < 0 || !subdir) {
            perror(program);
            exit(1);
        }