git: 242923eb848a - main - loader tslog: Don't use sprintf
Colin Percival
cperciva at FreeBSD.org
Sat Sep 25 03:23:56 UTC 2021
The branch main has been updated by cperciva:
URL: https://cgit.FreeBSD.org/src/commit/?id=242923eb848a94d657344c2ff45c4f15433cdf3c
commit 242923eb848a94d657344c2ff45c4f15433cdf3c
Author: Colin Percival <cperciva at FreeBSD.org>
AuthorDate: 2021-09-25 03:19:38 +0000
Commit: Colin Percival <cperciva at FreeBSD.org>
CommitDate: 2021-09-25 03:23:37 +0000
loader tslog: Don't use sprintf
Instead, append the log entry "manually".
MFC after: 1 week
Sponsored by: https://www.patreon.com/cperciva
---
stand/libsa/tslog.c | 50 +++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 43 insertions(+), 7 deletions(-)
diff --git a/stand/libsa/tslog.c b/stand/libsa/tslog.c
index c6164aab02a9..a2791f9dc9c4 100644
--- a/stand/libsa/tslog.c
+++ b/stand/libsa/tslog.c
@@ -42,6 +42,24 @@ static char * tslog_buf = NULL;
static size_t tslog_buflen = 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
tslog_setbuf(void * buf, size_t len)
{
@@ -69,16 +87,34 @@ tslog(const char * type, const char * f, const char * s)
#else
uint64_t tsc = 0;
#endif
- int len;
/* If we have no buffer, do nothing. */
if (tslog_buf == NULL)
return;
- /* Append to existing buffer, if we have enough space. */
- len = snprintf(&tslog_buf[tslog_bufpos],
- tslog_buflen - tslog_bufpos, "0x0 %llu %s %s%s%s\n",
- (unsigned long long)tsc, type, f, s ? " " : "", s ? s : "");
- if ((len > 0) && (tslog_bufpos + len <= tslog_buflen))
- tslog_bufpos += len;
+ /* Check that we have enough space. */
+ if (tslog_buflen - tslog_bufpos < 32 + strlen(type) + strlen(f) +
+ (s ? strlen(s) : 0))
+ return;
+
+ /* 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;
}
More information about the dev-commits-src-all
mailing list