summaryrefslogtreecommitdiff
path: root/kern
diff options
context:
space:
mode:
authorAlfred M. Szmidt <ams@gnu.org>2004-12-05 14:29:43 +0000
committerThomas Schwinge <tschwinge@gnu.org>2009-06-18 00:15:26 +0200
commit849488a953c0c98fbe5a473689bd25d5494b5927 (patch)
tree65c2a8097eb4987fdae9fe110486a66fb7d5ef56 /kern
parent2316d2aa6b9bbbe3f3ad7c604d0cc18000d47b81 (diff)
2004-09-08 Neal H. Walfield <neal@cs.uml.edu>
* linux/dev/kernel/printk.c: Include <kern/assert.h>. (printk): Use vsnprintf, not linux_vsprintf to avoid buffer overruns. * kern/printf.c (struct vsnprintf_cookie): New structure. (snputc): New function. (vsnprintf): Likewise.
Diffstat (limited to 'kern')
-rw-r--r--kern/printf.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/kern/printf.c b/kern/printf.c
index 85e6278..e6478ae 100644
--- a/kern/printf.c
+++ b/kern/printf.c
@@ -579,6 +579,34 @@ sprintf(char *buf, const char *fmt, ...)
return (buf - start);
}
+struct vsnprintf_cookie
+{
+ char *buf;
+ int index;
+ int max_len;
+};
+
+static void
+snputc(char c, vm_offset_t arg)
+{
+ struct vsnprintf_cookie *cookie = (void *) arg;
+
+ if (cookie->index < cookie->max_len)
+ cookie->buf[cookie->index ++] = c;
+}
+
+int
+vsnprintf(char *buf, int size, const char *fmt, va_list args)
+{
+ struct vsnprintf_cookie cookie
+ = { .buf = buf, .index = 0, .max_len = size };
+
+ _doprnt (fmt, &args, snputc, 16, (vm_offset_t)&cookie);
+ cookie.buf[cookie.index] = '\0';
+
+ return cookie.index;
+}
+
void safe_gets(str, maxlen)
char *str;