From 15f61c58dbb39ad08e0b1434439ee2868ddc1490 Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Sat, 8 Apr 2023 08:16:03 -0700 Subject: [PATCH] Fix bugs in 1990/theorem In this case these were not features. The most important bug fix actually prevented the entry from working properly: the generated code for the Fibonacci sequence just printed 0 over and over again. Added a segfault prevention here as it seemed like if I'm going to fix it I might as well fix it properly. Removed this bug from bugs.md. A set of possible issues with the char m[][] array was also including the size limit in the fgets() calls (that was my fault due to missing the proper type of m at the time). Still might be good to test with longer lines of input. Commands with known output are correct nonetheless. The other bug was due to the gets() to fgets() change where blank lines were added where they shouldn't be. Now one can do ls | ./sorter and get no blank lines after each file in the listing. --- 1990/theorem/.gitignore | 2 ++ 1990/theorem/README.md | 20 +++++++++++++------- 1990/theorem/theorem.c | 14 +++++++------- bugs.md | 5 ----- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/1990/theorem/.gitignore b/1990/theorem/.gitignore index 47cb1bda8..12683bf7d 100644 --- a/1990/theorem/.gitignore +++ b/1990/theorem/.gitignore @@ -3,3 +3,5 @@ fibonacci.c mariano sorter sorter.c +theorem_bkp +theorem_bkp.c diff --git a/1990/theorem/README.md b/1990/theorem/README.md index d85a4675f..7353b46b2 100644 --- a/1990/theorem/README.md +++ b/1990/theorem/README.md @@ -11,13 +11,19 @@ USA make all -[Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) fixed some segfaults -under modern (and in some cases earlier) systems with this entry. Originally we -noted that the 4 trailing args '0 0 0 0' were required on systems that dump core -when NULL is dereferenced but this problem showed itself in modern systems even -with the 4 '0 0 0 0'. Finally he changed this program to use `fgets()` not -`gets()` to make it safer and to prevent a warning about `gets()` at linking or -runtime. Thank you Cody for your assistance! +[Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) fixed some bugs that +impacted the usability of this program including some segfaults under modern +systems (and possibly in some cases earlier systems) with this entry. +Originally we noted that the 4 trailing args '0 0 0 0' were required on systems +that dump core when NULL is dereferenced but this problem showed itself in +modern systems even with the 4 '0 0 0 0'. He also fixed the code so that the +generated `fibonacci.c` actually works; before it just printed `0` over and over +again (since it did not work anyway a segfault prevention was added here). He +also some array addressing (some of which might not be strictly necessary but as +he was testing the `fibonacci.c` bug he ended up changing it anyway). Finally he +changed this program to use `fgets()` not `gets()` to make it safer and to +prevent a warning about `gets()` at linking or runtime. Thank you Cody for your +assistance! [Yusuke Endoh](/winners.html#Yusuke_Endoh) pointed out that `atof` nowadays needs `#include ` which was used in order to get this to work diff --git a/1990/theorem/theorem.c b/1990/theorem/theorem.c index db1d0de8b..a6d30ebcd 100644 --- a/1990/theorem/theorem.c +++ b/1990/theorem/theorem.c @@ -21,7 +21,7 @@ if(R=='/')*g/=u; if(R=='^')P(g,*g,u); C w(g,R,u)float*g,u;char R; -/**/{int b,f;A=atoi(++a);b=atoi(++a);while((f=A+b)<15000){printf("%d\n",f);A=b;b=f;}} +/**/{int b,f;if(A>2){A=atoi(a[1]);b=atoi(a[2]);while((f=A+b)<15000){printf("%d\n",f);A=b;b=f;}}} main(A,a)int A;char*a[]; o o if(!strcmp(*++a,"-r"))S(); @@ -46,14 +46,14 @@ W=D=1; ; while(W!=1) o o -strcpy(j+m,v); +strcpy(j[m],v); o if((j-=W)<=W)break; -strcpy(j+m,m+j-W); +strcpy(j[m],m[j-W]); C -while(strcmp(m+j-W,v)>0) +while(strcmp(m[j-W],v)>0) j=i; -strcpy(v,i+m); +strcpy(v,i[m]); C for(i=(W/=3)-1;++i