Skip to content

Commit

Permalink
use snprintf and strncpy in place of sprintf and strcpy
Browse files Browse the repository at this point in the history
This is both good practice and nice for OpenBSD users, who will no
longer get the nag message to not use sprintf/strcpy every time they
link against jansson. It's worth noting that the existing code seems
safe to me - bounds checks were already happening before the actual
calls - and that this is for extra security.
  • Loading branch information
haldean committed Apr 30, 2015
1 parent 7676001 commit 95dd927
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ static int dump_string(const char *str, size_t len, json_dump_callback_t dump, v
/* codepoint is in BMP */
if(codepoint < 0x10000)
{
sprintf(seq, "\\u%04X", codepoint);
snprintf(seq, 13, "\\u%04X", codepoint);
length = 6;
}

Expand All @@ -143,7 +143,7 @@ static int dump_string(const char *str, size_t len, json_dump_callback_t dump, v
first = 0xD800 | ((codepoint & 0xffc00) >> 10);
last = 0xDC00 | (codepoint & 0x003ff);

sprintf(seq, "\\u%04X\\u%04X", first, last);
snprintf(seq, 13, "\\u%04X\\u%04X", first, last);
length = 12;
}

Expand Down
6 changes: 3 additions & 3 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ void jsonp_error_set_source(json_error_t *error, const char *source)

length = strlen(source);
if(length < JSON_ERROR_SOURCE_LENGTH)
strcpy(error->source, source);
strncpy(error->source, source, length + 1);
else {
size_t extra = length - JSON_ERROR_SOURCE_LENGTH + 4;
strcpy(error->source, "...");
strcpy(error->source + 3, source + extra);
strncpy(error->source, "...", 3);
strncpy(error->source + 3, source + extra, length - extra + 1);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ int hashtable_set(hashtable_t *hashtable,

pair->hash = hash;
pair->serial = serial;
strcpy(pair->key, key);
strncpy(pair->key, key, len + 1);
pair->value = value;
list_init(&pair->list);

Expand Down

0 comments on commit 95dd927

Please sign in to comment.