summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Bratch <luke@bratch.co.uk>2019-05-18 19:03:48 +0100
committerLuke Bratch <luke@bratch.co.uk>2019-05-18 19:03:48 +0100
commitbff1674c746edc0b71b022a8d48f739d76b89641 (patch)
tree9786d12214a2d2bc912e10a32b7f99da534f767a
parent95ff740e8238799cd70d829053704b9c366fc59f (diff)
Add underflow safety check in appendcrlf().
-rw-r--r--functions.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/functions.c b/functions.c
index ffd94f1..bc4680d 100644
--- a/functions.c
+++ b/functions.c
@@ -78,8 +78,11 @@ int getstdin(char *prompt, char *buff, size_t sz) {
// Append CR-LF to the end of a string (after cleaning up any existing trailing CR or LF)
void appendcrlf(char *string) {
// Make sure it doesn't already end with CR or LF
- while (string[strlen(string) - 1] == '\r' || string[strlen(string) - 1] == '\n') {
- string[strlen(string) - 1] = '\0';
+ // (But only if string is at least two characters long already)
+ if (strlen(string) >= 2) {
+ while (string[strlen(string) - 1] == '\r' || string[strlen(string) - 1] == '\n') {
+ string[strlen(string) - 1] = '\0';
+ }
}
int startlen = strlen(string);