Commit 242923eb authored by Colin Percival's avatar Colin Percival
Browse files

loader tslog: Don't use sprintf

Instead, append the log entry "manually".

MFC after:	1 week
Sponsored by:	https://www.patreon.com/cperciva
parent 6687410a
...@@ -42,6 +42,24 @@ static char * tslog_buf = NULL; ...@@ -42,6 +42,24 @@ static char * tslog_buf = NULL;
static size_t tslog_buflen = 0; static size_t tslog_buflen = 0;
static size_t tslog_bufpos = 0; static size_t tslog_bufpos = 0;
static size_t
tsccat(char * buf, uint64_t tsc)
{
size_t len;
/* Handle upper digits. */
if (tsc >= 10)
len = tsccat(buf, tsc / 10);
else
len = 0;
/* Write the last digit. */
buf[len] = "0123456789"[tsc % 10];
/* Return the length written. */
return (len + 1);
}
void void
tslog_setbuf(void * buf, size_t len) tslog_setbuf(void * buf, size_t len)
{ {
...@@ -69,16 +87,34 @@ tslog(const char * type, const char * f, const char * s) ...@@ -69,16 +87,34 @@ tslog(const char * type, const char * f, const char * s)
#else #else
uint64_t tsc = 0; uint64_t tsc = 0;
#endif #endif
int len;
/* If we have no buffer, do nothing. */ /* If we have no buffer, do nothing. */
if (tslog_buf == NULL) if (tslog_buf == NULL)
return; return;
/* Append to existing buffer, if we have enough space. */ /* Check that we have enough space. */
len = snprintf(&tslog_buf[tslog_bufpos], if (tslog_buflen - tslog_bufpos < 32 + strlen(type) + strlen(f) +
tslog_buflen - tslog_bufpos, "0x0 %llu %s %s%s%s\n", (s ? strlen(s) : 0))
(unsigned long long)tsc, type, f, s ? " " : "", s ? s : ""); return;
if ((len > 0) && (tslog_bufpos + len <= tslog_buflen))
tslog_bufpos += len; /* Append to existing buffer. */
strcpy(&tslog_buf[tslog_bufpos], "0x0 ");
tslog_bufpos += 4;
tslog_bufpos += tsccat(&tslog_buf[tslog_bufpos], tsc);
strcpy(&tslog_buf[tslog_bufpos], " ");
tslog_bufpos += 1;
strcpy(&tslog_buf[tslog_bufpos], type);
tslog_bufpos += strlen(type);
strcpy(&tslog_buf[tslog_bufpos], " ");
tslog_bufpos += 1;
strcpy(&tslog_buf[tslog_bufpos], f);
tslog_bufpos += strlen(f);
if (s != NULL) {
strcpy(&tslog_buf[tslog_bufpos], " ");
tslog_bufpos += 1;
strcpy(&tslog_buf[tslog_bufpos], s);
tslog_bufpos += strlen(s);
}
strcpy(&tslog_buf[tslog_bufpos], "\n");
tslog_bufpos += 1;
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment