From 07f59467a14267ca271bc2a070a69dc232a7bb65 Mon Sep 17 00:00:00 2001 From: PartialVolume <22084881+PartialVolume@users.noreply.github.com> Date: Tue, 30 Dec 2025 22:24:49 +0000 Subject: [PATCH] Improve str_truncate Check start_column < wcols and issue error Reduce output length by 1 to take care a situation where output_length maybe provided by strlen which excludes the null terminator and sizeof which may or may count the terminator. --- src/gui.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/gui.c b/src/gui.c index 9dcc3ab..822018d 100644 --- a/src/gui.c +++ b/src/gui.c @@ -8266,16 +8266,22 @@ char* str_truncate( int wcols, int start_column, const char* input, char* output */ int length, idx = 0; - - length = wcols - start_column - 1; - idx = 0; - while( idx < output_length && idx < length ) + if( start_column < wcols ) { - output[idx] = input[idx]; - idx++; + length = wcols - start_column - 1; + idx = 0; + while( idx < output_length - 1 && idx < length && input[idx] != 0 ) + { + output[idx] = input[idx]; + idx++; + } + /* terminate the string */ + output[idx] = 0; + } + else + { + strncpy( output, "Error:start_column>=wcols", output_length - 1 ); } - /* terminate the string */ - output[idx] = 0; return output; }