From 5bbdf553ccb993ff50260791db1eb234d9355fab Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Tue, 28 Mar 2023 09:36:43 -0700 Subject: [PATCH] Fix segfault in 1994/schnitzi - use fgets() An example file that would cause the entry to crash is the Makefile. Now it won't crash. Strangely without increasing the buffer size but even using fgets() the Makefile did crash the entry. Changing the buffer size is not a problem but whether or not some other larger file will cause a problem or not I do not know. I don't see how it would but I don't see how this happened either. --- 1994/schnitzi/README.md | 28 +++++++++++++++++++++------- 1994/schnitzi/schnitzi.c | 8 ++++---- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/1994/schnitzi/README.md b/1994/schnitzi/README.md index a43988962..e7733f2dc 100644 --- a/1994/schnitzi/README.md +++ b/1994/schnitzi/README.md @@ -10,22 +10,36 @@ make all -### To run +## To run: ./schnitzi < textfile -## Judges' comments +[Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) fixed the entry to not +overflow the buffer (and segfault) by changing the call of `gets()` to +`fgets()`. He also increased the buffer size. Prior to this fix one could not +redirect the Makefile into this program, as an example problem. Thank you Cody +for your assistance! - Try: + +## Try: ./schnitzi < /etc/motd + ./schnitzi < /etc/passwd + + + +## Judges' comments: + + +Try: + ./schnitzi < schnitzi.c - Compare the output of the program with its source. Sure the output - is different, but there is more than meets the diff's eye. Can you - find out why? +Compare the output of the program with its source. Sure the output +is different, but there is more than meets the `diff`'s eye. Can you +find out why? -## Author's comments +## Author's comments: SPOILER: diff --git a/1994/schnitzi/schnitzi.c b/1994/schnitzi/schnitzi.c index 45a5365e4..2b1440fd5 100644 --- a/1994/schnitzi/schnitzi.c +++ b/1994/schnitzi/schnitzi.c @@ -20,12 +20,12 @@ h mh111 de{l) +di (+'( d+% c n+ */ n = 0 ; main ( ) { char v [ - 100 ] , s; - int t[100 ] , - u[100 ] ; u + 1000 ] , s; + int t[100 ] , + u[100 ] ; u [ 0 ]=ftell(stdin ) ; - while(gets (v ) ) + while(fgets(v,100,stdin )&&((v[strlen(v)-1]='\0',1)) ) {t [ r ]= strlen ( v);y=