Merge pull request #1144 from xexyl/1991-dds-fix-clang

This commit is contained in:
Landon Curt Noll authored and GitHub committed 2023-10-21 14:57:56 -07:00
commit 924feb058b
7 files changed
+50 -103

No files matched your search

-2
View File
@@ -1,6 +1,4 @@
dds
dds.alt
a
a.c
a.out
dds.orig
+2 -5
View File
@@ -114,8 +114,8 @@ OBJ= ${PROG}.o
DATA=
TARGET= ${PROG}
#
ALT_OBJ= ${PROG}.alt.o
ALT_TARGET= ${PROG}.alt
ALT_OBJ=
ALT_TARGET=
#################
@@ -132,9 +132,6 @@ all: data ${TARGET}
${PROG}: ${PROG}.c
${CC} ${CFLAGS} $< -o $@ ${LDFLAGS}
a: a.c
${CC} ${CFLAGS} -Wno-return-type $< -o a.out ${LDFLAGS}
# alternative executable
#
alt: data ${ALT_TARGET}
+1 -61
View File
@@ -12,9 +12,6 @@ Mastodon: <https://mstdn.social/@DSpinellis>
make all
```
There is an alt version for those who use `clang`. See [Alternate
code](#alternate-code) below.
## To run:
```sh
@@ -22,23 +19,9 @@ code](#alternate-code) below.
./a.out 2>/dev/null
```
The segfault which was always triggered was due to the code `*q>' '&&(*q)--;`.
This seems odd at first glance but it's pointing to `s` which was read-only
memory as a `char *s`. It's now a `char s[]`.
NOTE: for `clang` you'll likely have to use the alternate code described below.
## Try:
With gcc, make and run as follows:
```sh
make dds
```
For example, the author suggests trying:
The author suggests trying:
```sh
./dds LANDER.BAS 2>/dev/null
@@ -55,49 +38,6 @@ You'll get errors yes but what does the generated file look like? What about
other types of files?
### Alternate code:
This alternate code allows this entry to work successfully even with clang.
Unfortunately it's something of a hack or maybe even a kludge. This however
seems important because it works well and allows for not messing with the `s`
string which is quite complicated. Even changing the length could break
functionality and indeed the length would have to be longer for this to work
with clang.
How does it work? The original code (which you will see in [dds.c](dds.c)) does
a `return system(q-6);` which equates to `return system("cc a.c");` but clang by
default, at least in macOS, has default `-Werror` and there were some warnings.
This means that the compilation failed with clang (because it didn't use `make`
so no [Makefile](Makefile) was used). In particular it would fail (at least with
[LANDER.BAS](LANDER.BAS)) due to a return from `main()` without a return value
and the use of functions not yet declared.
Now as noted since the string `s` is complicated and to not inadvertently mess
something up it was changed so that the code uses `system("make a");` and then an
appropriate rule was added to the Makefile. Thus for the alternative version you
now need both `make` and `cc`. If you use gcc you can just use the original
version.
#### To build:
```sh
make alt
```
#### To run:
```sh
./dds.alt some_basic_program.bas 2>/dev/null
./a.out 2>/dev/null
```
#### Try:
```sh
./dds LANDER.BAS 2>/dev/null
./a.out 2>/dev/null
```
### INABIAF - it's not a bug it's a feature! :-)
Please note that if the BASIC file cannot be opened for reading or the output
-17
View File
@@ -1,17 +0,0 @@
#include <stdio.h>
#define Q r=R[*p++-'0'];while(
#define B ;break;case
char s[]="Qjou!s\\311^-g\\311^-n\\311^-c\\::^-q-ma%mO1JBHm%BQ-aP1J[O1HB%[Q<nbj\
o)*|gps)<<*txjudi)m*|aQdbtf!::::;sfuvso<aQefgbvmu;aQ<m,,a%CQ<csfbla%bQ<aN2!Q\
\ndbtf!aP2Q;m>aP2Q<a%!D12J!JGJHJOJQJFJSJJJMHS%HD12D12N3!N4\nJUJT%UQm>aP4HC%T\
Qs\\q,,^>m,2<m>aP4HC%SD12N1\nJNQm>s\\..q^aHC%NHb%GN1!D32P3%RN1UP1D12JPQUaP1H\
R%PN4\nQ<g\\(aP3Q(^>aP2Q,2<n\\(aP3Q(^>aP4Hb%OD12D12N2!N3\nJVP3Q,,<jg)aP3Q=>n\
\\(aP3Q(^*m>g\\(aP3Q(^<fmtf!m,,aHC%QN1!N1\nJ#Qqsjoug)#&e]o#-aP1Q*aHb%#Qqvut)\
aP1Q*aHb%FN1\nQm>::::aHC%VP3Q>bupj)hfut)c**aHb%JD12JON1!Qjg)a%LN1UP1D12JIQUa\
P1HL%IQ*m>aN2!N2\nP2Q<fmtf!m,,aHC%MN1!N2>P2Q>aN2\nP2Hbdd!b/d";int k;char R[4][99]
;main(c,v)char**v;{char*p,*r,*q;for(q=s;*q;q++)*q>' '&&(*q)--;{FILE*i=fopen(v
[1],"r"),*o=fopen(q-3,"w");for(p=s;;p++)switch(*p++){B'M':Q(k=fgetc(i))!=EOF
&&k!=*p)*r++=k;if(k==EOF){fputs("}}\n",o);fclose(o);return system("make a");}*r=0
B'P':while(*p!='`')fputc(*p++,o)B'O':Q*r)fputc(*r++,o);p--B'C':k=0;Q k<*p-'0'
)(*r++=fgetc(i),k++);*r=0 B'I':k= *p;if(**R==k)goto G B'G':k= *p;G:p=s;while(
*p!='$'||p[1]!= k)p++;p++B'N':R[*p-'0'][0]++;}}}
+3 -2
View File
@@ -8,10 +8,11 @@ Qs\\q,,^>m,2<m>aP4HC%SD12N1\nJNQm>s\\..q^aHC%NHb%GN1!D32P3%RN1UP1D12JPQUaP1H\
R%PN4\nQ<g\\(aP3Q(^>aP2Q,2<n\\(aP3Q(^>aP4Hb%OD12D12N2!N3\nJVP3Q,,<jg)aP3Q=>n\
\\(aP3Q(^*m>g\\(aP3Q(^<fmtf!m,,aHC%QN1!N1\nJ#Qqsjoug)#&e]o#-aP1Q*aHb%#Qqvut)\
aP1Q*aHb%FN1\nQm>::::aHC%VP3Q>bupj)hfut)c**aHb%JD12JON1!Qjg)a%LN1UP1D12JIQUa\
P1HL%IQ*m>aN2!N2\nP2Q<fmtf!m,,aHC%MN1!N2>P2Q>aN2\nP2Hbdd!b/d";int k;char R[4][99]
P1HL%IQ*m>aN2!N2\nP2Q<fmtf!m,,aHC%MN1!N2>P2Q>aN2\nP2Hbdd!.Xop.fssps!.Xop.sfu\
vso.uzqf!.Xop.jnqmjdju.gvodujpo.efdmbsbujpo!b/d";int k;char R[4][99]
;main(c,v)char**v;{char*p,*r,*q;for(q=s;*q;q++)*q>' '&&(*q)--;{FILE*i=fopen(v
[1],"r"),*o=fopen(q-3,"w");for(p=s;;p++)switch(*p++){B'M':Q(k=fgetc(i))!=EOF
&&k!=*p)*r++=k;if(k==EOF){fputs("}}\n",o);fclose(o);return system(q-6);}*r=0
&&k!=*p)*r++=k;if(k==EOF){fputs("}}\n",o);fclose(o);return system(q-69);}*r=0
B'P':while(*p!='`')fputc(*p++,o)B'O':Q*r)fputc(*r++,o);p--B'C':k=0;Q k<*p-'0'
)(*r++=fgetc(i),k++);*r=0 B'I':k= *p;if(**R==k)goto G B'G':k= *p;G:p=s;while(
*p!='$'||p[1]!= k)p++;p++B'N':R[*p-'0'][0]++;}}}
+5 -6
View File
@@ -685,7 +685,6 @@ This entry will also enter an infinite loop if input is not a number > 0.
# 1991
## [1991/dds](1991/dds/dds.c) ([README.md)(1991/dds/README.md))
## [1991/dds.alt](1991/dds/dds.alt.c) ([README.md)(1991/dds/README.md))
## STATUS: INABIAF - please **DO NOT** fix
If the BASIC file cannot be opened for reading or the output file cannot be
@@ -693,14 +692,14 @@ opened for writing the program will very likely crash or do something funny.
This is not a bug but a feature. Please do not fix this except for the
challenge to yourself.
Please also note that for `clang` you have to use [dds.alt](1991/dds/dds.alt.c) not
[dds.c](1991/dds/dds.c).
## STATUS: uses gets() - change to fgets() if possible
That being said the compiled code uses `gets()` not `fgets()`. Can you fix this?
You might find that the `s` array is relevant but if you do change it please
make sure to test all functionality!
It's quite complicated to do: the `s` array is certainly relevant and you can
see a bit of the magic in the [thanks](/thanks-for-fixes.md) and the README.md
file for how it works. It's easy enough to get the code to refer to `fgets()`
and call it correctly but it might take more work to get the generated code
sorted. This will be looked at later.
## [1991/westley](1991/westley/westley.c) ([README.md](1991/westley/README.md))
## STATUS: INABIAF - please **DO NOT** fix
+39 -10
View File
@@ -15,10 +15,11 @@ responsible for many of the improvements including many **very complicated bug
fixes** like [1988/phillipps](1988/phillipps/README.md),
[2001/anonymous](2001/anonymous/README.md) and
[2004/burley](2004/burley/README.md), making entries like
[1986/wall](1986/wall/README.md) not require `-traditional-cpp` (all **very
complicated fixes**), fixing entries to compile with clang, porting entries to
macOS, some being **very complicated** like
[1998/schweikh1](1998/schweikh1/README.md), fixing code like
[1986/wall](1986/wall/README.md) not need `-traditional-cpp` (all **very
complicated fixes**), fixing entries to work with clang (some being **very
complicated** like [1991/dds](1991/dds/README.md)), porting entries to
macOS (some being **very complicated** like
[1998/schweikh1](1998/schweikh1/README.md)), fixing code like
[2001/herrmann2](2001/herrmann2/README.md) to work in both 32-bit/64-bit which
*can be* **very complicated**, providing alternate code where useful/necessary,
fixing where possible dead links or removing them, typo/consistency fixes,
@@ -787,14 +788,42 @@ slightly more like the original, even though it's unused.
## [1991/dds](1991/dds/dds.c) ([README.md](1991/dds/README.md]))
Cody fixed a segfault that prevented this entry from working in any condition
and he also made an [alternate version](1991/dds/dds.alt.c) that works with
`clang`.
and he also made it work for clang. Clang (at least in some systems?) defaults
to having `-Wno-error` and the code that the entry generates had some warnings
that were causing compilation to fail as it just ran `cc a.c`. It ran it by what
was once `system(q-6);` but with clang this was not enough..
The alternate code, described in the README.md file, is what is needed
for clang. Reading it might be instructive even if you have gcc.
The following had to be added to the string `s` (which to fix the segfault was
changed from `char*s` to `char s[]`):
```
!.Xop.fssps!.Xop.sfuvso.uzqf!.Xop.jnqmjdju.gvodujpo.efdmbsbujpo
```
and then the call to `system()` had to be changed to:
```c
system(q-69);
```
An important point is that the placement of this string in the `s` array does
matter. This is because of the code:
```c
*o=fopen(q-3,"w");
```
which means the file name that translates to `a.c` has to be at the end of the
string. Thus the end of the string actually looks like:
```
!.Xop.fssps!.Xop.sfuvso.uzqf!.Xop.jnqmjdju.gvodujpo.efdmbsbujpo!b/d
```
With these changes in place it will compile and work with both gcc and clang.
The key to the string is that it rotates the character by `+1`. This was not
immediately clear until reading the author's remarks so there was an alt version
that was something of a kludge, running `make a` instead.
Later on Cody improved the fix for clang by a slight modification to the
Makefile so that the compiled program is the same as with gcc: `a.out`.
## [1991/fine](1991/fine/fine.c) ([README.md](1991/fine/README.md))