Make format_text() more secure

This commit is contained in:
Kovid Goyal 2019-08-28 05:41:20 +05:30
parent b016353809
commit 90a985b73a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -261,9 +261,13 @@ static inline const char*
format_text(const char *src) { format_text(const char *src) {
static char buf[256]; static char buf[256];
char *p = buf; char *p = buf;
const char *last_char = buf + sizeof(buf) - 1;
if (!src[0]) return "<none>"; if (!src[0]) return "<none>";
while (*src) { while (*src) {
p += snprintf(p, sizeof(buf) - (p - buf), "0x%x ", (unsigned char)*(src++)); int num = snprintf(p, sizeof(buf) - (p - buf), "0x%x ", (unsigned char)*(src++));
if (num < 0) return "<error>";
if (p + num >= last_char) break;
p += num;
} }
if (p != buf) *(--p) = 0; if (p != buf) *(--p) = 0;
return buf; return buf;