Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys Use gets_s instead of gets. The x86 bootloader prompt is...
details: https://anonhg.NetBSD.org/src/rev/37c8fc8fcb41
branches: trunk
changeset: 815855:37c8fc8fcb41
user: maxv <maxv%NetBSD.org@localhost>
date: Sun Jun 05 13:33:03 2016 +0000
description:
Use gets_s instead of gets. The x86 bootloader prompt is easy to
overflow.
diffstat:
sys/arch/i386/stand/lib/bootmenu.c | 4 +-
sys/arch/i386/stand/lib/menuutils.c | 4 +-
sys/lib/libsa/gets.c | 60 ++++++++++++++++++++++++++++++++++++-
sys/lib/libsa/stand.h | 4 +-
4 files changed, 66 insertions(+), 6 deletions(-)
diffs (132 lines):
diff -r d3b75cd5dbbd -r 37c8fc8fcb41 sys/arch/i386/stand/lib/bootmenu.c
--- a/sys/arch/i386/stand/lib/bootmenu.c Sun Jun 05 11:01:39 2016 +0000
+++ b/sys/arch/i386/stand/lib/bootmenu.c Sun Jun 05 13:33:03 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: bootmenu.c,v 1.14 2014/08/10 07:40:49 isaki Exp $ */
+/* $NetBSD: bootmenu.c,v 1.15 2016/06/05 13:33:03 maxv Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -178,7 +178,7 @@
printf("\nOption: [%d]:",
bootcfg_info.def + 1);
- gets(input);
+ gets_s(input, sizeof(input));
choice = getchoicefrominput(input, bootcfg_info.def);
} else if (bootcfg_info.timeout == 0)
choice = bootcfg_info.def;
diff -r d3b75cd5dbbd -r 37c8fc8fcb41 sys/arch/i386/stand/lib/menuutils.c
--- a/sys/arch/i386/stand/lib/menuutils.c Sun Jun 05 11:01:39 2016 +0000
+++ b/sys/arch/i386/stand/lib/menuutils.c Sun Jun 05 13:33:03 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: menuutils.c,v 1.4 2014/04/06 19:11:26 jakllsch Exp $ */
+/* $NetBSD: menuutils.c,v 1.5 2016/06/05 13:33:03 maxv Exp $ */
/*
* Copyright (c) 1996, 1997
@@ -71,7 +71,7 @@
input[0] = '\0';
printf("> ");
- gets(input);
+ gets_s(input, sizeof(input));
/*
* Skip leading whitespace.
diff -r d3b75cd5dbbd -r 37c8fc8fcb41 sys/lib/libsa/gets.c
--- a/sys/lib/libsa/gets.c Sun Jun 05 11:01:39 2016 +0000
+++ b/sys/lib/libsa/gets.c Sun Jun 05 13:33:03 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: gets.c,v 1.10 2007/11/24 13:20:55 isaki Exp $ */
+/* $NetBSD: gets.c,v 1.11 2016/06/05 13:33:03 maxv Exp $ */
/*-
* Copyright (c) 1993
@@ -85,3 +85,61 @@
}
/*NOTREACHED*/
}
+
+void
+gets_s(char *buf, size_t size)
+{
+ int c;
+ char *lp;
+
+ for (lp = buf;;) {
+ if (lp - buf == size) {
+ lp--;
+ *lp = '\0';
+ return;
+ }
+ switch (c = getchar() & 0177) {
+ case '\n':
+ case '\r':
+ *lp = '\0';
+ putchar('\n');
+ return;
+ case '\b':
+ case '\177':
+ if (lp > buf) {
+ lp--;
+ putchar('\b');
+ putchar(' ');
+ putchar('\b');
+ }
+ break;
+#if HASH_ERASE
+ case '#':
+ if (lp > buf)
+ --lp;
+ break;
+#endif
+ case 'r' & 037: {
+ char *p;
+
+ putchar('\n');
+ for (p = buf; p < lp; ++p)
+ putchar(*p);
+ break;
+ }
+#if AT_ERASE
+ case '@':
+#endif
+ case 'u' & 037:
+ case 'w' & 037:
+ lp = buf;
+ putchar('\n');
+ break;
+ default:
+ *lp++ = c;
+ putchar(c);
+ break;
+ }
+ }
+ /*NOTREACHED*/
+}
diff -r d3b75cd5dbbd -r 37c8fc8fcb41 sys/lib/libsa/stand.h
--- a/sys/lib/libsa/stand.h Sun Jun 05 11:01:39 2016 +0000
+++ b/sys/lib/libsa/stand.h Sun Jun 05 13:33:03 2016 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: stand.h,v 1.79 2014/08/10 07:40:49 isaki Exp $ */
+/* $NetBSD: stand.h,v 1.80 2016/06/05 13:33:03 maxv Exp $ */
/*
* Copyright (c) 1999 Christopher G. Demetriou. All rights reserved.
@@ -78,6 +78,7 @@
#ifdef LIBSA_RENAME_PRINTF
#define getchar libsa_getchar
#define gets libsa_gets
+#define gets_s libsa_gets_s
#define printf libsa_printf
#define putchar libsa_putchar
#define vprintf libsa_vprintf
@@ -258,6 +259,7 @@
__attribute__((__format__(__printf__, 3, 0)));
void twiddle(void);
void gets(char *);
+void gets_s(char *, size_t);
int getfile(char *prompt, int mode);
char *strerror(int);
__dead void exit(int);
Home |
Main Index |
Thread Index |
Old Index