From bff1674c746edc0b71b022a8d48f739d76b89641 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Sat, 18 May 2019 19:03:48 +0100 Subject: Add underflow safety check in appendcrlf(). --- functions.c | 7 +++++-- 1 file 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); -- cgit v1.2.3