From fdfcc15b3c8a1be1133784f1d523d8e04cd35431 Mon Sep 17 00:00:00 2001 From: guenther Date: Sun, 20 Apr 2014 22:31:55 +0000 Subject: [PATCH] Eliminate duplicated logic by switching from malloc+snprintf to asprintf ok beck@ --- lib/libcrypto/dso/dso_dlfcn.c | 34 +++++++++++---------------- lib/libssl/src/crypto/dso/dso_dlfcn.c | 34 +++++++++++---------------- 2 files changed, 28 insertions(+), 40 deletions(-) diff --git a/lib/libcrypto/dso/dso_dlfcn.c b/lib/libcrypto/dso/dso_dlfcn.c index 245b9b14789..62b826ea430 100644 --- a/lib/libcrypto/dso/dso_dlfcn.c +++ b/lib/libcrypto/dso/dso_dlfcn.c @@ -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); } diff --git a/lib/libssl/src/crypto/dso/dso_dlfcn.c b/lib/libssl/src/crypto/dso/dso_dlfcn.c index 245b9b14789..62b826ea430 100644 --- a/lib/libssl/src/crypto/dso/dso_dlfcn.c +++ b/lib/libssl/src/crypto/dso/dso_dlfcn.c @@ -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); } -- 2.20.1