Merge pull request #2180 from janisozaur/safe_strncpy

Fixes to safe_strncpy
This commit is contained in:
Ted John 2015-11-01 22:52:17 +00:00
commit 5914558575
2 changed files with 6 additions and 2 deletions

View File

@ -188,7 +188,7 @@ void console_draw(rct_drawpixelinfo *dpi)
int lineLength = min(sizeof(lineBuffer) - (size_t)utf8_get_codepoint_length(FORMAT_GREEN), (size_t)(nextLine - ch)); int lineLength = min(sizeof(lineBuffer) - (size_t)utf8_get_codepoint_length(FORMAT_GREEN), (size_t)(nextLine - ch));
lineCh = lineBuffer; lineCh = lineBuffer;
lineCh = utf8_write_codepoint(lineCh, FORMAT_GREEN); lineCh = utf8_write_codepoint(lineCh, FORMAT_GREEN);
safe_strncpy(lineCh, ch, lineLength); safe_strncpy(lineCh, ch, CONSOLE_BUFFER_SIZE);
lineCh[lineLength] = 0; lineCh[lineLength] = 0;
gfx_draw_string(dpi, lineBuffer, 255, x, y); gfx_draw_string(dpi, lineBuffer, 255, x, y);

View File

@ -183,6 +183,10 @@ int strcicmp(char const *a, char const *b)
char *safe_strncpy(char * destination, const char * source, size_t size) char *safe_strncpy(char * destination, const char * source, size_t size)
{ {
if (size == 0)
{
return destination;
}
char *result = destination; char *result = destination;
bool terminated = false; bool terminated = false;
for (size_t i = 0; i < size; i++) for (size_t i = 0; i < size; i++)
@ -198,7 +202,7 @@ char *safe_strncpy(char * destination, const char * source, size_t size)
} }
if (!terminated) if (!terminated)
{ {
destination[size - 1] = '\0'; result[size - 1] = '\0';
log_warning("Truncating string %s to %d bytes.", destination, size); log_warning("Truncating string %s to %d bytes.", destination, size);
} }
return result; return result;