summaryrefslogtreecommitdiff
path: root/sbin/fdisk
diff options
context:
space:
mode:
authorMoritz Jodeit <moritz@cvs.openbsd.org>2006-10-24 17:30:46 +0000
committerMoritz Jodeit <moritz@cvs.openbsd.org>2006-10-24 17:30:46 +0000
commit1b0ec1a61bf3f0e8c464e5be57640c31aa697948 (patch)
tree5aaffd171b76a8cc9445de654672275df07eaf08 /sbin/fdisk
parent781b3f7a216ba1b0979218aa783352a43fff826a (diff)
Check strlen(buf) to be > 0 before accessing buf[strlen(buf)-1].
OK ray@ cloder@
Diffstat (limited to 'sbin/fdisk')
-rw-r--r--sbin/fdisk/misc.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/sbin/fdisk/misc.c b/sbin/fdisk/misc.c
index 6aa71e3558d..3c9be4c0e2c 100644
--- a/sbin/fdisk/misc.c
+++ b/sbin/fdisk/misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.c,v 1.16 2005/11/21 01:59:24 krw Exp $ */
+/* $OpenBSD: misc.c,v 1.17 2006/10/24 17:30:45 moritz Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -67,11 +67,14 @@ int
ask_cmd(cmd_t *cmd)
{
char lbuf[100], *cp, *buf;
+ size_t lbuflen;
/* Get input */
if (fgets(lbuf, sizeof lbuf, stdin) == NULL)
errx(1, "eof");
- lbuf[strlen(lbuf)-1] = '\0';
+ lbuflen = strlen(lbuf);
+ if (lbuflen > 0 && lbuf[lbuflen - 1] == '\n')
+ lbuf[lbuflen - 1] = '\0';
/* Parse input */
buf = lbuf;
@@ -90,6 +93,7 @@ ask_num(const char *str, int flags, int dflt, int low, int high,
void (*help)(void))
{
char lbuf[100], *cp;
+ size_t lbuflen;
int num;
do {
@@ -104,7 +108,9 @@ again:
if (fgets(lbuf, sizeof lbuf, stdin) == NULL)
errx(1, "eof");
- lbuf[strlen(lbuf)-1] = '\0';
+ lbuflen = strlen(lbuf);
+ if (lbuflen > 0 && lbuf[lbuflen - 1] == '\n')
+ lbuf[lbuflen - 1] = '\0';
if (help && lbuf[0] == '?') {
(*help)();