fix segfaults casused by size_t format specifier
authorNorbert Federa <norbert.federa@thincast.com>
Fri, 27 May 2016 13:53:49 +0000 (15:53 +0200)
committerNorbert Federa <norbert.federa@thincast.com>
Fri, 27 May 2016 13:55:28 +0000 (15:55 +0200)
win32/msvc cc does not recognize the %z format specifier which caused
invalid references and segfaults on win32.
Until FreeRDP gets format specifier macros we'll cast size_t to
unsigned long and use the %lu specifier.

Also simplified winpr_backtrace_symbols() a little bit and fixed it
to allocate the correct amount of bytes for the return buffer.

libfreerdp/primitives/test/TestPrimitivesYUV.c
winpr/libwinpr/utils/debug.c
winpr/libwinpr/utils/print.c
winpr/libwinpr/utils/test/TestBacktrace.c
winpr/libwinpr/utils/test/TestImage.c
winpr/libwinpr/utils/wlog/wlog.c

index a4b34f7..9100100 100644 (file)
@@ -25,7 +25,7 @@ static BOOL similar(const BYTE* src, const BYTE* dst, size_t size)
 
                if (abs(diff) > 2)
                {
-                       fprintf(stderr, "%zd %02X : %02X diff=%lf\n", x, val1, val2, diff);
+                       fprintf(stderr, "%lu %02X : %02X diff=%lf\n", (unsigned long)x, val1, val2, diff);
                        return FALSE;
                }
        }
@@ -66,8 +66,8 @@ static BOOL check_padding(const BYTE* psrc, size_t size, size_t padding, const c
                        while((x < halfPad) && (*esrc++ != 'A'))
                                x++;
 
-                       fprintf(stderr, "Buffer underflow detected %02x != %02X %s [%zd-%zd]\n",
-                               d, 'A', buffer, start, x);
+                       fprintf(stderr, "Buffer underflow detected %02x != %02X %s [%lu-%lu]\n",
+                               d, 'A', buffer, (unsigned long)start, (unsigned long)x);
                        return FALSE;
                }
                if(d != 'A')
@@ -76,8 +76,8 @@ static BOOL check_padding(const BYTE* psrc, size_t size, size_t padding, const c
                        while((x < halfPad) && (*esrc++ != 'A'))
                                x++;
 
-                       fprintf(stderr, "Buffer overflow detected %02x != %02X %s [%zd-%zd]\n",
-                               d, 'A', buffer, start, x);
+                       fprintf(stderr, "Buffer overflow detected %02x != %02X %s [%lu-%lu]\n",
+                               d, 'A', buffer, (unsigned long)start, (unsigned long)x);
                        return FALSE;
                }
        }
index e6538a5..91f1843 100644 (file)
@@ -357,25 +357,22 @@ char** winpr_backtrace_symbols(void* buffer, size_t* used)
        {
                size_t line_len = (data->max > 1024) ? data->max : 1024;
                size_t i;
-               char* lines = calloc(data->used + 1, sizeof(char *) * line_len);
-               char** vlines = (char**) lines;
+               size_t array_size = data->used * sizeof(char*);
+               size_t lines_size = data->used * line_len;
+               char **vlines = calloc(1, array_size + lines_size);
+
                backtrace_symbol_t* symbols = calloc(data->used, sizeof(backtrace_symbol_t));
 
-               if (!lines || !symbols)
+               if (!vlines || !symbols)
                {
-                       if (lines)
-                               free(lines);
-
-                       if (symbols)
-                               free(symbols);
-
+                       free(vlines);
+                       free(symbols);
                        return NULL;
                }
 
-               /* To allow a char** malloced array to be returned, allocate n+1 lines
-               * and fill in the first lines[i] char with the address of lines[(i+1) * 1024] */
+               /* Set the pointers in the allocated buffer's initial array section */
                for (i = 0; i < data->used; i++)
-                       vlines[i] = &lines[(i + 1) * line_len];
+                       vlines[i] = (char*)vlines + array_size + i * line_len;
 
                fkt->get_backtrace_symbols(data->buffer, data->used, symbols);
 
@@ -388,7 +385,7 @@ char** winpr_backtrace_symbols(void* buffer, size_t* used)
                if (used)
                        *used = data->used;
 
-               return (char**) lines;
+               return vlines;
        }
 #elif (defined(_WIN32) || defined(_WIN64)) && !defined(_UWP)
        {
@@ -396,22 +393,17 @@ char** winpr_backtrace_symbols(void* buffer, size_t* used)
                size_t line_len = 1024;
                HANDLE process = GetCurrentProcess();
                t_win_stack* data = (t_win_stack*) buffer;
-               char *lines = calloc(data->used + 1, sizeof(char*) * line_len);
-               char **vlines = (char**) lines;
+               size_t array_size = data->used * sizeof(char*);
+               size_t lines_size = data->used * line_len;
+               char **vlines = calloc(1, array_size + lines_size);
                SYMBOL_INFO* symbol = calloc(sizeof(SYMBOL_INFO) + line_len * sizeof(char), 1);
                IMAGEHLP_LINE64* line = (IMAGEHLP_LINE64*) calloc(1, sizeof(IMAGEHLP_LINE64));
 
-               if (!lines || !symbol || !line)
+               if (!vlines || !symbol || !line)
                {
-                               if (lines)
-                                       free(lines);
-
-                               if (symbol)
-                                       free(symbol);
-
-                               if (line)
-                                       free(line);
-
+                               free(vlines);
+                               free(symbol);
+                               free(line);
                                return NULL;
                }
 
@@ -419,10 +411,9 @@ char** winpr_backtrace_symbols(void* buffer, size_t* used)
                symbol->MaxNameLen = line_len;
                symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
 
-               /* To allow a char** malloced array to be returned, allocate n+1 lines
-               * and fill in the first lines[i] char with the address of lines[(i+1) * 1024] */
+               /* Set the pointers in the allocated buffer's initial array section */
                for (i = 0; i < data->used; i++)
-                       vlines[i] = &lines[(i + 1) * line_len];
+                       vlines[i] = (char*)vlines + array_size + i * line_len;
 
                for (i = 0; i < data->used; i++)
                {
@@ -445,7 +436,7 @@ char** winpr_backtrace_symbols(void* buffer, size_t* used)
                        free(symbol);
                        free(line);
 
-                       return (char**) lines;
+                       return vlines;
        }
 #else
        LOGF(support_msg);
@@ -504,7 +495,7 @@ void winpr_log_backtrace(const char* tag, DWORD level, DWORD size)
        if (msg)
        {
                for (x=0; x<used; x++)
-                       WLog_LVL(tag, level, "%zd: %s\n", x, msg[x]);
+                       WLog_LVL(tag, level, "%lu: %s\n", (unsigned long)x, msg[x]);
        }
        winpr_backtrace_free(stack);
 }
index 71ba26a..803b836 100644 (file)
@@ -43,7 +43,7 @@ void winpr_HexDump(const char* tag, UINT32 level, const BYTE* data, int length)
 
        if (!buffer)
        {
-               WLog_ERR(tag, "malloc(%zd) failed with [%d] %s", blen, errno, strerror(errno));
+               WLog_ERR(tag, "malloc(%lu) failed with [%d] %s", (unsigned long)blen, errno, strerror(errno));
                return;
        }
 
@@ -84,7 +84,7 @@ void winpr_CArrayDump(const char* tag, UINT32 level, const BYTE* data, int lengt
 
        if (!buffer)
        {
-               WLog_ERR(tag, "malloc(%zd) failed with [%d] %s", llen, errno, strerror(errno));
+               WLog_ERR(tag, "malloc(%lu) failed with [%d] %s", (unsigned long)llen, errno, strerror(errno));
                return;
        }
 
index 3104db1..ea73192 100644 (file)
@@ -17,7 +17,7 @@ int TestBacktrace(int argc, char* argv[])
        if (msg)
        {
                for (x=0; x<used; x++)
-                       printf("%zd: %s\n", x, msg[x]);
+                       printf("%lu: %s\n", (unsigned long)x, msg[x]);
                rc = 0;
        }
        winpr_backtrace_symbols_fd(stack, fileno(stdout));
index 7c98333..5c8c809 100644 (file)
@@ -14,7 +14,7 @@ static void *read_image(const char *src, size_t *size)
        int success = 0;
        void *a = NULL;
        long src_size;
-       FILE *fsrc = fopen(src, "r");
+       FILE *fsrc = fopen(src, "rb");
 
        if (!fsrc)
        {
@@ -40,13 +40,13 @@ static void *read_image(const char *src, size_t *size)
 
        if (!a)
        {
-               fprintf(stderr, "Failed malloc %zd bytes\n", src_size);
+               fprintf(stderr, "Failed malloc %ld bytes\n", src_size);
                goto cleanup;
        }
 
        if (fread(a, sizeof(char), src_size, fsrc) != src_size)
        {
-               fprintf(stderr, "Failed read %zd bytes\n", src_size);
+               fprintf(stderr, "Failed read %ld bytes\n", src_size);
                goto cleanup;
        }
 
index a4c8e46..5653da0 100644 (file)
@@ -104,7 +104,7 @@ static BOOL log_recursion(LPCSTR file, LPCSTR fkt, int line)
                return FALSE;
 
        for (i=0; i<used; i++)
-               if (fprintf(stderr, "%s: %zd: %s\n", fkt, i, msg[i]) < 0)
+               if (fprintf(stderr, "%s: %lu: %s\n", fkt, (unsigned long)i, msg[i]) < 0)
                        return FALSE;
 
 #endif