Skip to content

Commit 7448e9e

Browse files
committed
Fix potential buffer overflow
1 parent 5370f51 commit 7448e9e

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

minichlink/minichlink.c

+14-20
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ int main( int argc, char ** argv )
380380
{
381381
uint8_t buffer[256];
382382
#if TERMINAL_INPUT_BUFFER
383-
char print_buf[300]; // Buffer that is filled with everything and will be written to stdout (basically it's for formatting)
383+
char print_buf[TERMINAL_BUFFER_SIZE]; // Buffer that is filled with everything and will be written to stdout (basically it's for formatting)
384384
uint8_t update = 0;
385385
#endif
386386
if( !IsGDBServerInShadowHaltState( dev ) )
@@ -417,17 +417,11 @@ int main( int argc, char ** argv )
417417
}
418418
if( to_send == 0 )
419419
{
420-
strcpy( print_buf, TERMINAL_CLEAR_CUR );
421-
strcat( print_buf, TERMIANL_INPUT_SENT );
422-
strcat( print_buf, input_buf );
423-
strcat( print_buf, "\n" );
424-
strcat( print_buf, pline_buf );
425-
strcat( print_buf, TERMINAL_SEND_LABEL );
420+
snprintf(print_buf, TERMINAL_BUFFER_SIZE - 1, "%s%s%s\n%s%s", TERMINAL_CLEAR_CUR, TERMIANL_INPUT_SENT, input_buf, pline_buf, TERMINAL_SEND_LABEL);
426421
fwrite( print_buf, strlen( print_buf ), 1, stdout );
427422
fflush( stdout );
428423
input_pos = 0;
429-
input_buf[0] = 0;
430-
// memset( input_buf, 0, sizeof( input_buf ) );
424+
memset( input_buf, 0, sizeof( input_buf ) );
431425
}
432426
appendword |= i + 4;
433427
}
@@ -448,10 +442,10 @@ int main( int argc, char ** argv )
448442
#if TERMINAL_INPUT_BUFFER
449443
if( ( r == -1 || r == 0 ) && update > 0 )
450444
{
451-
strcpy( print_buf, TERMINAL_CLEAR_CUR );
452-
if ( to_send > 0 ) strcat( print_buf, TERMINAL_DIM );
453-
strcat( print_buf, TERMINAL_SEND_LABEL );
454-
strcat( print_buf, input_buf );
445+
strncpy( print_buf, TERMINAL_CLEAR_CUR, TERMINAL_BUFFER_SIZE - 1 );
446+
if ( to_send > 0 ) strncat( print_buf, TERMINAL_DIM, TERMINAL_BUFFER_SIZE - 1 - strlen(print_buf) );
447+
strncat( print_buf, TERMINAL_SEND_LABEL, TERMINAL_BUFFER_SIZE - 1 - strlen(print_buf) );
448+
strncat( print_buf, input_buf, TERMINAL_BUFFER_SIZE - 1 - strlen(print_buf) );
455449
fwrite( print_buf, strlen( print_buf ), 1, stdout );
456450
fflush( stdout );
457451
}
@@ -471,13 +465,13 @@ int main( int argc, char ** argv )
471465
#if TERMINAL_INPUT_BUFFER
472466
uint8_t new_line = 0;
473467
if( buffer[r - 1] == '\n' ) new_line = 1;
474-
if( new_line == 0 ) strcpy( print_buf, TERMINAL_CLEAR_PREV ); // Go one line up and erase it
475-
else strcpy( print_buf, TERMINAL_CLEAR_CUR ); // Go to the start of the line and erase it
476-
strncat( pline_buf, (char *)buffer, r ); // Add newely received chars to line buffer
477-
strcat( print_buf, pline_buf ); // Add line to buffer
478-
if( to_send > 0 ) strcat( print_buf, TERMINAL_DIM );
479-
strcat( print_buf, TERMINAL_SEND_LABEL ); // Print styled "Send" label
480-
strcat( print_buf, input_buf ); // Print current input
468+
if( new_line == 0 ) strncpy( print_buf, TERMINAL_CLEAR_PREV, TERMINAL_BUFFER_SIZE - 1 ); // Go one line up and erase it
469+
else strncpy( print_buf, TERMINAL_CLEAR_CUR, TERMINAL_BUFFER_SIZE - 1 ); // Go to the start of the line and erase it
470+
strncat( pline_buf, (char *)buffer, TERMINAL_BUFFER_SIZE - 1 - strlen(print_buf) ); // Add newely received chars to line buffer
471+
strncat( print_buf, pline_buf, TERMINAL_BUFFER_SIZE - 1 - strlen(print_buf) ); // Add line to buffer
472+
if( to_send > 0 ) strncat( print_buf, TERMINAL_DIM, TERMINAL_BUFFER_SIZE - 1 - strlen(print_buf) );
473+
strncat( print_buf, TERMINAL_SEND_LABEL, TERMINAL_BUFFER_SIZE - 1 - strlen(print_buf) ); // Print styled "Send" label
474+
strncat( print_buf, input_buf, TERMINAL_BUFFER_SIZE - 1 - strlen(print_buf) ); // Print current input
481475
fwrite( print_buf, strlen( print_buf ), 1, stdout );
482476
print_buf[0] = 0;
483477
if( new_line == 1 ) pline_buf[0] = 0;

minichlink/minichlink.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,11 @@ struct InternalState
195195
#define DLLDECORATE
196196
#endif
197197

198-
#define TERMINAL_INPUT_BUFFER 1
198+
#ifndef TERMINAL_INPUT_BUFFER
199+
#define TERMINAL_INPUT_BUFFER 0
200+
#endif
201+
202+
#define TERMINAL_BUFFER_SIZE 512
199203

200204
#define STR_(x) #x
201205
#define STR(x) STR_(x)
@@ -206,7 +210,6 @@ struct InternalState
206210
#endif
207211

208212
#define TERMIANL_INPUT_SENT "\x1b[1F\x1b[2K\x1b[2K\033[38;" STR(TERMINAL_ACCENT_COLOR) "m> "
209-
#define TERMINAL_SEND_LABEL_N "\033[7m\033[1m\033[38;" STR(TERMINAL_ACCENT_COLOR) "mSend:\x1b[0m "
210213
#define TERMINAL_SEND_LABEL "\n\x1b[2K\033[7m\033[1m\033[38;" STR(TERMINAL_ACCENT_COLOR) "mSend:\x1b[0m "
211214
#define TERMINAL_SEND_BUSY "\n\x1b[2K\033[7m\033[1m\033[2m\033[38;" STR(TERMINAL_ACCENT_COLOR) "mSend:\x1b[0m "
212215
#define TERMINAL_CLEAR_PREV "\x1b[1F\x1b[2K"

0 commit comments

Comments
 (0)