From 42d78da74e3311fcf4e2239faafdaf8c7dfe71de Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Mon, 25 Jun 2018 13:19:26 -0400 Subject: [PATCH] Fix some bugs in the new mpw_strings & mpw_strncasecmp. --- platform-independent/c/core/src/mpw-util.c | 23 +++++++++++----------- platform-independent/c/core/src/mpw-util.h | 4 ++-- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/platform-independent/c/core/src/mpw-util.c b/platform-independent/c/core/src/mpw-util.c index e103e65d..55cbf05a 100644 --- a/platform-independent/c/core/src/mpw-util.c +++ b/platform-independent/c/core/src/mpw-util.c @@ -67,17 +67,19 @@ const char **mpw_strings(size_t *count, const char *strings, ...) { va_list args; va_start( args, strings ); const char **array = NULL; - size_t arraySize = 0; - for (const char *string; (string = va_arg( args, const char * ));) { - size_t cursor = arraySize; - if (!mpw_realloc( &array, &arraySize, sizeof(string) )) { - mpw_free( &array, arraySize ); + size_t size = 0; + for (const char *string = strings; string; (string = va_arg( args, const char * ))) { + size_t cursor = size / sizeof( *array ); + if (!mpw_realloc( &array, &size, sizeof( string ) )) { + mpw_free( &array, size ); + *count = 0; return NULL; } array[cursor] = string; } va_end( args ); + *count = size / sizeof( *array ); return array; } @@ -518,12 +520,9 @@ char *mpw_strndup(const char *src, size_t max) { int mpw_strncasecmp(const char *s1, const char *s2, size_t max) { - if (s1 && s2 && max) - for (; --max > 0; ++s1, ++s2) { - int cmp = tolower( *(unsigned char *)s1 ) - tolower( *(unsigned char *)s2 ); - if (!cmp || *s1 == '\0') - return cmp; - } + int cmp = 0; + for (; !cmp && max-- > 0 && s1 && s2; ++s1, ++s2) + cmp = tolower( *(unsigned char *)s1 ) - tolower( *(unsigned char *)s2 ); - return 0; + return cmp; } diff --git a/platform-independent/c/core/src/mpw-util.h b/platform-independent/c/core/src/mpw-util.h index bf95b6cb..eb8a3c8b 100644 --- a/platform-independent/c/core/src/mpw-util.h +++ b/platform-independent/c/core/src/mpw-util.h @@ -118,9 +118,9 @@ bool mpw_push_int( /** Reallocate the given buffer from the given size by adding the delta size. * On success, the buffer size pointer will be updated to the buffer's new size * and the buffer pointer may be updated to a new memory address. - * On failure, the buffer and pointers will remain unaffected. + * On failure, the pointers will remain unaffected. * @param buffer A pointer to the buffer to reallocate. - * @param bufferSize A pointer to the buffer's actual size. + * @param bufferSize A pointer to the buffer's current size. * @param deltaSize The amount to increase the buffer's size by. * @return true if successful, false if reallocation failed. */