From c1c8197c3430cf50991b62538677ce4c9b19399d Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Wed, 4 Oct 2023 04:09:38 -0700 Subject: [PATCH] Add alt code to 1986/hague - fgets() The reason it is in alt code is because one of the forms of input (see below) will, although print out correct output, segfault after outputting the text. That is when one does something like: echo foo | ./hague.alt Whether it might be worth making it the entry itself with a note in the Makefile and README.md and bugs.md (because it does print out correct output) can be determined later - or not. This change required several files to be updated besides the new file hague.alt.c. --- 1986/hague/.gitignore | 1 + 1986/hague/Makefile | 4 ++-- 1986/hague/README.md | 30 +++++++++++++++++++++++++++--- 1986/hague/hague.alt.c | 28 ++++++++++++++++++++++++++++ 1986/hague/hague.c | 4 ++-- bugs.md | 22 +++++++++++++++++++--- faq.md | 24 ++++++++++++++++++++++++ thanks-for-fixes.md | 11 +++++++++++ 8 files changed, 114 insertions(+), 10 deletions(-) create mode 100644 1986/hague/hague.alt.c diff --git a/1986/hague/.gitignore b/1986/hague/.gitignore index 3bbef39c6..4f2188ba7 100644 --- a/1986/hague/.gitignore +++ b/1986/hague/.gitignore @@ -1,3 +1,4 @@ hague +hague.alt hague.orig prog.orig diff --git a/1986/hague/Makefile b/1986/hague/Makefile index 810b9c51f..d1a379b02 100644 --- a/1986/hague/Makefile +++ b/1986/hague/Makefile @@ -114,8 +114,8 @@ OBJ= ${PROG}.o DATA= TARGET= ${PROG} # -ALT_OBJ= -ALT_TARGET= +ALT_OBJ= ${PROG}.alt.o +ALT_TARGET= ${PROG}.alt ################# diff --git a/1986/hague/README.md b/1986/hague/README.md index d79cd8cc8..9b01d231f 100644 --- a/1986/hague/README.md +++ b/1986/hague/README.md @@ -9,9 +9,9 @@ UK make all ``` - -NOTE: this entry uses `gets()` so you might get a warning about this when -compiling and running this program with some systems. +There is an alternate version which uses `fgets()`. See the Alternate code +section below as well as the [bugs.md](/bugs.md) file for more details as to why +it is worth having as an alternate version. ## To run: @@ -29,6 +29,30 @@ International Obfuscated C Code Contest Jim Hague ``` +Also try: + +```sh +echo IOCCC | ./hague +``` + +### Alternate code: + +Whereas with some entries the change to `fgets()` (see the [FAQ](/faq.md) for +details on why this has been done) can be done in the original code this entry +is more complicated in that although both forms of input show the correct +output, one form segfaults after the output. + +If you wish to use this alternate code, do: + +```sh +make alt +``` + +Use `hague.alt` as you would `hague` above. As for which form will segfault +after outputting the text it is when text is piped into the program. Again see +the [bugs.md](/bugs.md) file for more details. + + ## Judges' remarks: diff --git a/1986/hague/hague.alt.c b/1986/hague/hague.alt.c new file mode 100644 index 000000000..c5dc9dba3 --- /dev/null +++ b/1986/hague/hague.alt.c @@ -0,0 +1,28 @@ +#define DIT ( +#define DAH ) +#define __DAH ++ +#define DITDAH * +#define DAHDIT for +#define DIT_DAH malloc +#define DAH_DIT fgets +#define _DAHDIT char +_DAHDIT _DAH_[]="ETIANMSURWDKGOHVFaLaPJBXCYZQb54a3d2f16g7c8a90l?e'b.s;i,d:" +;main DIT DAH{_DAHDIT +DITDAH _DIT,DITDAH DAH_,DITDAH DIT_, +DITDAH _DIT_,DITDAH DIT_DAH DIT +DAH,DITDAH DAH_DIT DIT DAH;DAHDIT +DIT _DIT=DIT_DAH DIT 81 DAH,DIT_=_DIT +__DAH;_DIT=DAH_DIT DIT _DIT,81,stdin DAH,_DIT[strlen(_DIT)-1]='\0',_DIT!=NULL;__DIT +DIT'\n'DAH DAH DAHDIT DIT DAH_=_DIT;DITDAH +DAH_;__DIT DIT DITDAH +_DIT_?_DAH DIT DITDAH DIT_ DAH:'?'DAH,__DIT +DIT' 'DAH,DAH_ __DAH DAH DAHDIT DIT +DITDAH DIT_=2,_DIT_=_DAH_; DITDAH _DIT_&&DIT +DITDAH _DIT_!=DIT DITDAH DAH_>='a'? DITDAH +DAH_&223:DITDAH DAH_ DAH DAH; DIT +DITDAH DIT_ DAH __DAH,_DIT_ __DAH DAH +DITDAH DIT_+= DIT DITDAH _DIT_>='a'? DITDAH _DIT_-'a':0 +DAH;}_DAH DIT DIT_ DAH{ __DIT DIT +DIT_>3?_DAH DIT DIT_>>1 DAH:'\0'DAH;return +DIT_&1?'-':'.';}__DIT DIT DIT_ DAH _DAHDIT +DIT_;{DIT void DAH write DIT 1,&DIT_,1 DAH;} diff --git a/1986/hague/hague.c b/1986/hague/hague.c index bf43532b3..c5dc9dba3 100644 --- a/1986/hague/hague.c +++ b/1986/hague/hague.c @@ -4,7 +4,7 @@ #define DITDAH * #define DAHDIT for #define DIT_DAH malloc -#define DAH_DIT gets +#define DAH_DIT fgets #define _DAHDIT char _DAHDIT _DAH_[]="ETIANMSURWDKGOHVFaLaPJBXCYZQb54a3d2f16g7c8a90l?e'b.s;i,d:" ;main DIT DAH{_DAHDIT @@ -12,7 +12,7 @@ DITDAH _DIT,DITDAH DAH_,DITDAH DIT_, DITDAH _DIT_,DITDAH DIT_DAH DIT DAH,DITDAH DAH_DIT DIT DAH;DAHDIT DIT _DIT=DIT_DAH DIT 81 DAH,DIT_=_DIT -__DAH;_DIT==DAH_DIT DIT _DIT DAH;__DIT +__DAH;_DIT=DAH_DIT DIT _DIT,81,stdin DAH,_DIT[strlen(_DIT)-1]='\0',_DIT!=NULL;__DIT DIT'\n'DAH DAH DAHDIT DIT DAH_=_DIT;DITDAH DAH_;__DIT DIT DITDAH _DIT_?_DAH DIT DITDAH DIT_ DAH:'?'DAH,__DIT diff --git a/bugs.md b/bugs.md index 706e02ddc..8d6fa91db 100644 --- a/bugs.md +++ b/bugs.md @@ -469,11 +469,27 @@ the judges and shouldn't be fixed. This entry uses `gets()` which is unsafe. In particular the buffer size is a mere 81 and it does not read a single string at command invocation but instead until the program is terminated. This size can be increased to start out but it -would be better if it used `fgets()`. This will be addressed in time but is -noted here as a reminder. +would be better if it used `fgets()`. The way this entry is formed has made it not as simple as some of the others -that have been addressed. +that have been addressed at least in part. It is easy to get it to use `fgets()` +and this has been done in the [alternate code](1986/hague/hague.alt.c) but it +will segfault after outputting the (correct) text in one of the forms. In +particular whereas this: + +```sh +./hague.alt +foo + +works the following will print out the correct output but then segfault: + +``` +```sh +echo foo | ./hague.alt +``` + +Whether or not it is worth making the entry itself (since the output is correct) +this alt version is to be determined later but it's might very well not be. ## [1986/holloway](1986/holloway/holloway.c) ([README.md](1986/holloway/README.md)) diff --git a/faq.md b/faq.md index eda2e2dde..901aae497 100644 --- a/faq.md +++ b/faq.md @@ -88,6 +88,30 @@ some entries you should look at the original code as in `winner.orig.c` or `prog.orig.c`. Sometimes the original is in an alt version like `winner.alt.c` or `prog.alt.c`. +## Q: Why have some entries that originally used `gets()` been modified to use `fgets()`? Doesn't this tamper with the entry too much? + +A fine line indeed has to be drawn here but it was decided that it is worth it +because of alarming warnings that can be displayed, in some systems at runtime +interspersed with the output of the program. + +For instance in macOS the entry [1990/tbr](1990/tbr/README.md) would output the +warning in such a way that caused confusing output for the entry. + +In some cases this is not so easy to fix and in one case at least there is an +alternate version that has the fix instead due to a problem it creates (correct +output but segfaults after the output in one of the forms of input). + +In some cases it is not possible to fix or at least highly unlikely and so those +have mainly not been touched except one that has had the buffer size increased +(which could be done for others that are not possible to change to `fgets()` but +this has not been done). + +In the future we, the judges, would prefer that entries use `fgets()` to prevent +these problems. + +NOTE: due to 'compatibility reasons' `fgets()` stores the newline and `gets()` +does not. We're not sure how this is compatibility but either way it can cause a +problem and it is this that has complicated some fixes. ## Q: I cannot get entry XYZZY from year 19xx to compile! diff --git a/thanks-for-fixes.md b/thanks-for-fixes.md index c4bb9563c..0df4ac00d 100644 --- a/thanks-for-fixes.md +++ b/thanks-for-fixes.md @@ -138,6 +138,17 @@ entry which can be found in [1985/sicherman/sicherman.orig.c](1985/sicherman/sicherman.orig.c). +## [1986/hague](1986/hague/hague.c) ([README.md](1986/hague/README.md])) + +Cody added alt code that uses `fgets()` to avoid alarming and annoying warnings +about `gets()` being unsafe. The reason this is alt code, at least for now, is +that although it will print the results in both forms of invocation, after the +output in one invocation form it will segfault. This might be addressed later on +or it might be decided that it is worth it, with a caveat in the Makefile or at +least the README.md file, since it still prints out the correct output, though +that seems like it might not be so ideal unless it can be fixed (which might be +done). See the [bugs.md](/bugs.md) file for details. + ## [1986/holloway](1986/holloway/holloway.c) ([README.md](1986/holloway/README.md])) Cody fixed this to compile and work with clang (it already worked with gcc).