fix segfaults casused by size_t format specifier

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.
This commit is contained in:
Norbert Federa 2016-05-27 15:53:49 +02:00
parent 00dd6f8c51
commit e8c4910e2e
6 changed files with 33 additions and 42 deletions

View 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;
}
}

View 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(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(vlines);
free(symbol);
if (line)
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);
}

View 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;
}

View 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));

View 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;
}

View 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