From 380d744d01f3054da8e6b76a3cb3c36ea25b557f Mon Sep 17 00:00:00 2001 From: krw Date: Tue, 24 Aug 2021 12:34:04 +0000 Subject: [PATCH] Trim leading/trailing whitespace from -e input to make command parsing more robust (e.g. 'edit 0 ' is now accepted) and strict (e.g. 'reinit gptx' is now rejected). Input which may want that whitespace is not trimmed (e.g. GPT partition names). Use consistent definition of whitespace corresponding to that used by isspace(). --- sbin/fdisk/cmd.c | 18 +++++++++--------- sbin/fdisk/misc.c | 24 ++++++++++++++++++------ sbin/fdisk/misc.h | 8 ++++++-- sbin/fdisk/user.c | 20 ++++++++------------ 4 files changed, 41 insertions(+), 29 deletions(-) diff --git a/sbin/fdisk/cmd.c b/sbin/fdisk/cmd.c index 11ee896a798..abcba16ea5e 100644 --- a/sbin/fdisk/cmd.c +++ b/sbin/fdisk/cmd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd.c,v 1.139 2021/08/15 13:45:42 krw Exp $ */ +/* $OpenBSD: cmd.c,v 1.140 2021/08/24 12:34:04 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -55,9 +55,9 @@ Xreinit(char *args, struct mbr *mbr) dogpt = 0; - if (strncasecmp(args, "gpt", 3) == 0) + if (strcasecmp(args, "gpt") == 0) dogpt = 1; - else if (strncasecmp(args, "mbr", 3) == 0) + else if (strcasecmp(args, "mbr") == 0) dogpt = 0; else if (strlen(args) > 0) { printf("Unrecognized modifier '%s'\n", args); @@ -115,7 +115,7 @@ Xswap(char *args, struct mbr *mbr) struct gpt_partition gg; to = args; - from = strsep(&to, " \t"); + from = strsep(&to, WHITESPACE); pt = parsepn(to); if (pt == -1) @@ -505,7 +505,7 @@ Xflag(char *args, struct mbr *mbr) int i, pn; flag = args; - part = strsep(&flag, " \t"); + part = strsep(&flag, WHITESPACE); pn = parsepn(part); if (pn == -1) @@ -586,7 +586,7 @@ ask_num(const char *str, int dflt, int low, int high) do { printf("%s [%d - %d]: [%d] ", str, low, high, dflt); - string_from_line(lbuf, sizeof(lbuf)); + string_from_line(lbuf, sizeof(lbuf), TRIMMED); if (lbuf[0] == '\0') { num = dflt; @@ -610,9 +610,9 @@ ask_pid(const int dflt, struct uuid *guid) do { printf("Partition id ('0' to disable) [01 - FF]: [%X] ", dflt); printf("(? for help) "); - string_from_line(lbuf, sizeof(lbuf)); + string_from_line(lbuf, sizeof(lbuf), TRIMMED); - if (lbuf[0] == '?') { + if (strcmp(lbuf, "?") == 0) { PRT_printall(); continue; } @@ -650,7 +650,7 @@ ask_string(const char *prompt, const char *oval) buf[0] = '\0'; printf("%s: [%s] ", prompt, oval ? oval : ""); - string_from_line(buf, sizeof(buf)); + string_from_line(buf, sizeof(buf), UNTRIMMED); if (buf[0] == '\0' && oval) strlcpy(buf, oval, sizeof(buf)); diff --git a/sbin/fdisk/misc.c b/sbin/fdisk/misc.c index 2c058fea652..91b191f0db3 100644 --- a/sbin/fdisk/misc.c +++ b/sbin/fdisk/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.80 2021/08/15 13:45:42 krw Exp $ */ +/* $OpenBSD: misc.c,v 1.81 2021/08/24 12:34:04 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -60,19 +60,31 @@ unit_lookup(const char *units) } void -string_from_line(char *buf, const size_t buflen) +string_from_line(char *buf, const size_t buflen, const int trim) { static char *line; static size_t sz; ssize_t len; + unsigned int i; len = getline(&line, &sz, stdin); if (len == -1) errx(1, "eof"); - line[strcspn(line, "\n")] = '\0'; - - strlcpy(buf, line, buflen); + switch (trim) { + case UNTRIMMED: + line[strcspn(line, "\n")] = '\0'; + strlcpy(buf, line, buflen); + break; + case TRIMMED: + for (i = strlen(line); i > 0; i--) { + if (isspace((unsigned char)line[i - 1]) == 0) + break; + line[i - 1] = '\0'; + } + strlcpy(buf, line + strspn(line, WHITESPACE), buflen); + break; + } } int @@ -122,7 +134,7 @@ getuint64(const char *prompt, uint64_t oval, const uint64_t minval, do { printf("%s [%llu - %llu]: [%llu] ", prompt, minval, maxval, oval); - string_from_line(buf, sizeof(buf)); + string_from_line(buf, sizeof(buf), TRIMMED); if (buf[0] == '\0') { rslt = snprintf(buf, sizeof(buf), "%llu", oval); diff --git a/sbin/fdisk/misc.h b/sbin/fdisk/misc.h index 14b6c2f2683..cd60e67c7b3 100644 --- a/sbin/fdisk/misc.h +++ b/sbin/fdisk/misc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.h,v 1.41 2021/08/15 13:45:42 krw Exp $ */ +/* $OpenBSD: misc.h,v 1.42 2021/08/24 12:34:04 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -31,9 +31,13 @@ extern struct unit_type unit_types[]; #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) #endif +#define WHITESPACE " \f\n\r\t\v" +#define UNTRIMMED 0 +#define TRIMMED 1 + /* Prototypes */ int unit_lookup(const char *); -void string_from_line(char *, const size_t); +void string_from_line(char *, const size_t, const int); int ask_yn(const char *); uint64_t getuint64(const char *, uint64_t, const uint64_t, const uint64_t); char *utf16le_to_string(const uint16_t *); diff --git a/sbin/fdisk/user.c b/sbin/fdisk/user.c index 4d751b9155c..8de642093d3 100644 --- a/sbin/fdisk/user.c +++ b/sbin/fdisk/user.c @@ -1,4 +1,4 @@ -/* $OpenBSD: user.c,v 1.73 2021/08/15 13:45:42 krw Exp $ */ +/* $OpenBSD: user.c,v 1.74 2021/08/24 12:34:04 krw Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -97,7 +97,7 @@ again: break; /* Quick hack to put in '?' == 'help' */ - if (!strcmp(cmd, "?")) + if (strcmp(cmd, "?") == 0) i = 0; if (i >= nitems(cmd_table) || (letoh64(gh.gh_sig) == @@ -106,7 +106,7 @@ again: continue; } - st = cmd_table[i].cmd_fcn(args, &mbr); + st = cmd_table[i].cmd_fcn(args ? args : "", &mbr); /* Update status */ if (st == CMD_EXIT) @@ -206,16 +206,12 @@ void ask_cmd(char **cmd, char **arg) { static char lbuf[100]; - size_t cmdstart, cmdend, argstart; - string_from_line(lbuf, sizeof(lbuf)); + string_from_line(lbuf, sizeof(lbuf), TRIMMED); - cmdstart = strspn(lbuf, " \t"); - cmdend = cmdstart + strcspn(&lbuf[cmdstart], " \t"); - argstart = cmdend + strspn(&lbuf[cmdend], " \t"); + *arg = lbuf; + *cmd = strsep(arg, WHITESPACE); - /* *cmd and *arg may be set to point at final NUL! */ - *cmd = &lbuf[cmdstart]; - lbuf[cmdend] = '\0'; - *arg = &lbuf[argstart]; + if (*arg != NULL) + *arg += strspn(*arg, WHITESPACE); } -- 2.20.1