Skip to content

Commit

Permalink
fix: Tighten up a few snprintf usages (#508)
Browse files Browse the repository at this point in the history
Check the return value in more places, and handle errors better.
Also, explicitly null-terminate in more places as some implementations
might not be well behaved.
  • Loading branch information
Swatinem authored Apr 8, 2021
1 parent 6d71367 commit 701215d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
19 changes: 16 additions & 3 deletions src/sentry_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ char *
sentry__msec_time_to_iso8601(uint64_t time)
{
char buf[255];
size_t buf_len = sizeof(buf);
time_t secs = time / 1000;
struct tm *tm;
#ifdef SENTRY_PLATFORM_WINDOWS
Expand All @@ -366,14 +367,26 @@ sentry__msec_time_to_iso8601(uint64_t time)
struct tm tm_buf;
tm = gmtime_r(&secs, &tm_buf);
#endif
size_t end = strftime(buf, sizeof buf, "%Y-%m-%dT%H:%M:%S", tm);
size_t written = strftime(buf, buf_len, "%Y-%m-%dT%H:%M:%S", tm);
if (written == 0) {
return NULL;
}

int msecs = time % 1000;
if (msecs) {
snprintf(buf + end, 10, ".%03d", msecs);
size_t rv = (size_t)snprintf(
buf + written, buf_len - written, ".%03d", msecs);
if (rv >= buf_len - written) {
return NULL;
}
written += rv;
}

strcat(buf, "Z");
if (written + 2 > buf_len) {
return NULL;
}
buf[written] = 'Z';
buf[written + 1] = '\0';
return sentry__string_clone(buf);
}

Expand Down
30 changes: 25 additions & 5 deletions src/sentry_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,12 @@ sentry__value_stringify(sentry_value_t value)
return sentry__string_clone(sentry_value_as_string(value));
default: {
char buf[50];
snprintf(buf, sizeof(buf), "%g", sentry_value_as_double(value));
size_t written = (size_t)sentry__snprintf_c(
buf, sizeof(buf), "%g", sentry_value_as_double(value));
if (written >= sizeof(buf)) {
return sentry__string_clone("");
}
buf[written] = '\0';
return sentry__string_clone(buf);
}
}
Expand Down Expand Up @@ -927,21 +932,35 @@ sentry_value_t
sentry__value_new_addr(uint64_t addr)
{
char buf[100];
snprintf(buf, sizeof(buf), "0x%llx", (unsigned long long)addr);
size_t written = (size_t)snprintf(
buf, sizeof(buf), "0x%llx", (unsigned long long)addr);
if (written >= sizeof(buf)) {
return sentry_value_new_null();
}
buf[written] = '\0';
return sentry_value_new_string(buf);
}

sentry_value_t
sentry__value_new_hexstring(const uint8_t *bytes, size_t len)
{
char *buf = sentry_malloc(len * 2 + 1);
size_t buf_len = len * 2 + 1;
char *buf = sentry_malloc(buf_len);
if (!buf) {
return sentry_value_new_null();
}
char *ptr = buf;
size_t written = 0;

for (size_t i = 0; i < len; i++) {
ptr += snprintf(ptr, 3, "%02hhx", bytes[i]);
size_t rv = (size_t)snprintf(
buf + written, buf_len - written, "%02hhx", bytes[i]);
if (rv >= buf_len - written) {
sentry_free(buf);
return sentry_value_new_null();
}
written += rv;
}
buf[written] = '\0';
return sentry__value_new_string_owned(buf);
}

Expand All @@ -953,6 +972,7 @@ sentry__value_new_uuid(const sentry_uuid_t *uuid)
return sentry_value_new_null();
}
sentry_uuid_as_string(uuid, buf);
buf[36] = '\0';
return sentry__value_new_string_owned(buf);
}

Expand Down
8 changes: 6 additions & 2 deletions src/transports/sentry_transport_curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,12 @@ sentry__curl_send_task(void *_envelope, void *_state)
headers = curl_slist_append(headers, "expect:");
for (size_t i = 0; i < req->headers_len; i++) {
char buf[255];
snprintf(buf, sizeof(buf), "%s:%s", req->headers[i].key,
req->headers[i].value);
size_t written = (size_t)snprintf(buf, sizeof(buf), "%s:%s",
req->headers[i].key, req->headers[i].value);
if (written >= sizeof(buf)) {
continue;
}
buf[written] = '\0';
headers = curl_slist_append(headers, buf);
}

Expand Down

0 comments on commit 701215d

Please sign in to comment.