Eliminate duplicated logic by switching from malloc+snprintf to asprintf
authorguenther <guenther@openbsd.org>
Sun, 20 Apr 2014 22:31:55 +0000 (22:31 +0000)
committerguenther <guenther@openbsd.org>
Sun, 20 Apr 2014 22:31:55 +0000 (22:31 +0000)
ok beck@

lib/libcrypto/dso/dso_dlfcn.c
lib/libssl/src/crypto/dso/dso_dlfcn.c

index 245b9b1..62b826e 100644 (file)
@@ -307,30 +307,24 @@ static char *
 dlfcn_name_converter(DSO *dso, const char *filename)
 {
        char *translated;
-       int len, rsize, transform;
-
-       len = strlen(filename);
-       rsize = len + 1;
-       transform = (strstr(filename, "/") == NULL);
-       if (transform) {
-               /* We will convert this to "%s.so" or "lib%s.so" etc */
-               rsize += DSO_extlen;    /* The length of ".so" */
+       int ret;
+
+       if (strchr(filename, '/') == NULL) {
+               /* Bare name, so convert to "%s.so" or "lib%s.so" */
                if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
-                       rsize += 3; /* The length of "lib" */
+                       ret = asprintf(&translated, "lib%s" DSO_ext, filename);
+               else
+                       ret = asprintf(&translated, "%s" DSO_ext, filename);
+               if (ret == -1)
+                       translated = NULL;
+       } else {
+               /* Full path, so just duplicate it */
+               translated = strdup(filename);
        }
-       translated = malloc(rsize);
-       if (translated == NULL) {
+
+       if (translated == NULL)
                DSOerr(DSO_F_DLFCN_NAME_CONVERTER,
                    DSO_R_NAME_TRANSLATION_FAILED);
-               return (NULL);
-       }
-       if (transform) {
-               if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
-                       snprintf(translated, rsize, "lib%s" DSO_ext, filename);
-               else
-                       snprintf(translated, rsize, "%s" DSO_ext, filename);
-       } else
-               snprintf(translated, rsize, "%s", filename);
        return (translated);
 }
 
index 245b9b1..62b826e 100644 (file)
@@ -307,30 +307,24 @@ static char *
 dlfcn_name_converter(DSO *dso, const char *filename)
 {
        char *translated;
-       int len, rsize, transform;
-
-       len = strlen(filename);
-       rsize = len + 1;
-       transform = (strstr(filename, "/") == NULL);
-       if (transform) {
-               /* We will convert this to "%s.so" or "lib%s.so" etc */
-               rsize += DSO_extlen;    /* The length of ".so" */
+       int ret;
+
+       if (strchr(filename, '/') == NULL) {
+               /* Bare name, so convert to "%s.so" or "lib%s.so" */
                if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
-                       rsize += 3; /* The length of "lib" */
+                       ret = asprintf(&translated, "lib%s" DSO_ext, filename);
+               else
+                       ret = asprintf(&translated, "%s" DSO_ext, filename);
+               if (ret == -1)
+                       translated = NULL;
+       } else {
+               /* Full path, so just duplicate it */
+               translated = strdup(filename);
        }
-       translated = malloc(rsize);
-       if (translated == NULL) {
+
+       if (translated == NULL)
                DSOerr(DSO_F_DLFCN_NAME_CONVERTER,
                    DSO_R_NAME_TRANSLATION_FAILED);
-               return (NULL);
-       }
-       if (transform) {
-               if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0)
-                       snprintf(translated, rsize, "lib%s" DSO_ext, filename);
-               else
-                       snprintf(translated, rsize, "%s" DSO_ext, filename);
-       } else
-               snprintf(translated, rsize, "%s", filename);
        return (translated);
 }