From d4d76daa1241c0aacd80ae60073abd852d048afe Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sat, 13 Jun 2015 00:44:47 +0200 Subject: [PATCH] Random cleanups --- libretro-common/compat/compat.c | 12 +++++++++--- libretro-common/hash/rhash.c | 2 +- libretro-common/net/net_http.c | 8 ++++---- libretro-common/string/stdstring.c | 17 +++++++++-------- libretro-common/string/string_list.c | 5 +++-- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/libretro-common/compat/compat.c b/libretro-common/compat/compat.c index 3b51123d7b..6adb54cb61 100644 --- a/libretro-common/compat/compat.c +++ b/libretro-common/compat/compat.c @@ -77,7 +77,8 @@ static int parse_short(const char *optstring, char * const *argv) { bool extra_opt, takes_arg, embedded_arg; const char *opt = NULL; - char arg = argv[0][1]; + char arg = argv[0][1]; + if (arg == ':') return '?'; @@ -161,8 +162,9 @@ static int parse_long(const struct option *longopts, char * const *argv) static void shuffle_block(char **begin, char **last, char **end) { - ptrdiff_t len = last - begin; + ptrdiff_t len = last - begin; const char **tmp = (const char**)calloc(len, sizeof(const char*)); + rarch_assert(tmp); memcpy(tmp, begin, len * sizeof(const char*)); @@ -176,6 +178,7 @@ int getopt_long(int argc, char *argv[], const char *optstring, const struct option *longopts, int *longindex) { int short_index, long_index; + (void)longindex; if (optind == 0) @@ -222,6 +225,7 @@ int getopt_long(int argc, char *argv[], static int casencmp(const char *a, const char *b, size_t n) { size_t i; + for (i = 0; i < n; i++) { int a_lower = tolower(a[i]); @@ -275,6 +279,7 @@ size_t strlcpy(char *dest, const char *source, size_t size) size_t strlcat(char *dest, const char *source, size_t size) { size_t len = strlen(dest); + dest += len; if (len > size) @@ -306,6 +311,7 @@ int rarch_strcasecmp__(const char *a, const char *b) { int a_ = tolower(*a); int b_ = tolower(*b); + if (a_ != b_) return a_ - b_; @@ -319,7 +325,7 @@ int rarch_strcasecmp__(const char *a, const char *b) char *rarch_strdup__(const char *orig) { size_t len = strlen(orig) + 1; - char *ret = (char*)malloc(len); + char *ret = (char*)malloc(len); if (!ret) return NULL; diff --git a/libretro-common/hash/rhash.c b/libretro-common/hash/rhash.c index cfe4f62c2c..9476f6aa1f 100644 --- a/libretro-common/hash/rhash.c +++ b/libretro-common/hash/rhash.c @@ -501,7 +501,7 @@ static void SHA1Input(SHA1Context *context, int sha1_calculate(const char *path, char *result) { - unsigned char buff[4096]; + unsigned char buff[4096] = {0}; SHA1Context sha; int rv = 1; int fd = open(path, O_RDONLY); diff --git a/libretro-common/net/net_http.c b/libretro-common/net/net_http.c index 3bb82fc826..be2e127d41 100644 --- a/libretro-common/net/net_http.c +++ b/libretro-common/net/net_http.c @@ -58,19 +58,19 @@ struct http_connection_t char *domain; char *location; char *urlcopy; - char* scan; + char *scan; int port; }; -static int net_http_new_socket(const char * domain, int port) +static int net_http_new_socket(const char *domain, int port) { - char portstr[16]; int fd; #ifndef _WIN32 struct timeval timeout; #endif struct addrinfo hints, *addr = NULL; + char portstr[16] = {0}; snprintf(portstr, sizeof(portstr), "%i", port); @@ -276,7 +276,7 @@ struct http_t *net_http_new(struct http_connection_t *conn) if (conn->port != 80) { - char portstr[16]; + char portstr[16] = {0}; snprintf(portstr, sizeof(portstr), ":%i", conn->port); net_http_send_str(fd, &error, portstr); diff --git a/libretro-common/string/stdstring.c b/libretro-common/string/stdstring.c index 75aa149542..abd39145e6 100644 --- a/libretro-common/string/stdstring.c +++ b/libretro-common/string/stdstring.c @@ -46,10 +46,10 @@ char *string_to_upper(char *s) char *string_replace_substring(const char *in, const char *pattern, const char *replacement) { - char *needle = NULL; - char *newstr = NULL; - char *head = NULL; - size_t pattern_len = 0; + char *needle = NULL; + char *newstr = NULL; + char *head = NULL; + size_t pattern_len = 0; size_t replacement_len = 0; /* if either pattern or replacement is NULL, @@ -57,18 +57,19 @@ char *string_replace_substring(const char *in, const char *pattern, const char * if (!pattern || !replacement) return strdup(in); - pattern_len = strlen(pattern); + pattern_len = strlen(pattern); replacement_len = strlen(replacement); - newstr = strdup(in); - head = newstr; + newstr = strdup(in); + head = newstr; while ((needle = strstr(head, pattern))) { - char* oldstr = newstr; + char *oldstr = newstr; size_t oldstr_len = strlen(oldstr); newstr = (char*)malloc(oldstr_len - pattern_len + replacement_len + 1); + if (!newstr) { /* Failed to allocate memory, diff --git a/libretro-common/string/string_list.c b/libretro-common/string/string_list.c index ec5e258d9b..b9c26cf713 100644 --- a/libretro-common/string/string_list.c +++ b/libretro-common/string/string_list.c @@ -108,7 +108,8 @@ struct string_list *string_list_new(void) bool string_list_append(struct string_list *list, const char *elem, union string_list_elem_attr attr) { - char *data_dup; + char *data_dup = NULL; + if (list->size >= list->cap && !string_list_capacity(list, list->cap * 2)) return false; @@ -250,7 +251,7 @@ bool string_list_find_elem_prefix(const struct string_list *list, const char *prefix, const char *elem) { size_t i; - char prefixed[PATH_MAX_LENGTH]; + char prefixed[PATH_MAX_LENGTH] = {0}; if (!list) return false;