From: Simon Wilkinson Date: Thu, 28 Feb 2013 12:15:29 +0000 (+0000) Subject: dumptool: Remove newlines safely X-Git-Tag: upstream/1.6.10_pre1^2~163 X-Git-Url: https://git.michaelhowe.org/gitweb/?a=commitdiff_plain;h=564573da48f61e5d96d5df017d25a39c930d566f;p=packages%2Fo%2Fopenafs.git dumptool: Remove newlines safely The code currently does fgets(cmdbuf, ... ); cmdbuf[strlen(cmdbuf - 1)] = '\0'; in order to remove new lines from cmdbuf. Coverity thinks there's a danger of strlen(cmdbuf) being 0, and thus the strlen being negative. That shouldn't happen, but if fgets hits EOF midway through a line, we might get a string that doesn't have a trailing '\n', and end up removing the wrong character. Tidy this up by checking that the string isn't 0 length, and that the character we're zapping is a newline. Caught by coverity (#985430) Reviewed-on: http://gerrit.openafs.org/9310 Reviewed-by: Derrick Brashear Tested-by: BuildBot Reviewed-by: Jeffrey Altman (cherry picked from commit 8f51502e5f45a43fba130d260813716be894d51e) Change-Id: Ie165c8e50c4071c07fffa220601c8b5e92ccf815 Reviewed-on: http://gerrit.openafs.org/11026 Reviewed-by: Chas Williams - CONTRACTOR Tested-by: BuildBot Reviewed-by: Andrew Deason Reviewed-by: Stephan Wiesand --- diff --git a/src/tests/dumptool.c b/src/tests/dumptool.c index 79c48cff6..f9871cd33 100644 --- a/src/tests/dumptool.c +++ b/src/tests/dumptool.c @@ -1215,7 +1215,8 @@ InteractiveRestore(FILE * f, VolumeDiskData * vol) printf("> "); while (fgets(cmdbuf, CMDBUFSIZE, stdin)) { - cmdbuf[strlen(cmdbuf) - 1] = '\0'; + if (strlen(cmdbuf) > 0 && cmdbuf[strlen(cmdbuf) - 1] == '\n') + cmdbuf[strlen(cmdbuf) - 1] = '\0'; if (strlen(cmdbuf) == 0) { printf("> ");