From 733a013ee75f43c4fe4a317a7806981f4e6dd8a1 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 28 May 2025 17:31:07 +0200 Subject: [PATCH 1/2] remove bugs related to uninitialized variables These bugs became visible when using optimization options. They were either not present unoptimized or very unlikely to occur. However, given the options, the resulting program would behave incorrectly and reliably break under all circumstances. The makefile will see the intended changes accordingly in the following commit to ensure compatibility. --- main.c | 31 +++++++++++++++++-------------- output.c | 1 + 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/main.c b/main.c index c2017bf..44b9a71 100644 --- a/main.c +++ b/main.c @@ -13,11 +13,12 @@ unsigned char sbuf[SXTETBUF]; int main(int argc, char *argv[]) { - int c, n, l; + int c, n, last; int dec, url, hlp, wrp; - char *prog = *argv; FILE *in, *out; + char *prog; + prog = *argv; dec = url = hlp = wrp = 0; while (--argc > 0 && (*++argv)[0] == '-') while ((c = *++argv[0])) @@ -42,6 +43,7 @@ main(int argc, char *argv[]) if (hlp) { fprintf(stdout, "Usage: %s -duhw infile outfile\n", prog); } else { + in = out = NULL; if (argc >= 1 && (in = fopen(*argv, "r")) == NULL) { fprintf(stderr, "%s: can't open %s\n", prog, *argv); exit(EXIT_FAILURE); @@ -51,22 +53,23 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } if (dec) { - while ((n = getsxts((argc >= 1) ? in : stdin, sbuf))) { + while ((n = getsxts((in) ? in : stdin, sbuf))) { n = decode(sbuf, n, obuf, url); - fwrite(obuf, sizeof(*obuf), n, (argc == 2) ? out : stdout); + fwrite(obuf, sizeof(*obuf), n, (out) ? out : stdout); } + } else if (wrp) { + last = 0; + while ((n = getocts((in) ? in : stdin, obuf))) { + n = encode(obuf, n, sbuf, url); + last = printw((out) ? out : stdout, sbuf, n); + } + if (last != '\n') + fprintf((out) ? out : stdout, "\n"); } else { - while ((n = getocts((argc >= 1) ? in : stdin, obuf))) { - if (wrp) { - n = encode(obuf, n, sbuf, url); - l = printw((argc == 2) ? out : stdout, sbuf, n); - } else { - encode(obuf, n, sbuf, url); - fprintf((argc == 2) ? out : stdout, "%s", sbuf); - } + while ((n = getocts((in) ? in : stdin, obuf))) { + encode(obuf, n, sbuf, url); + fprintf((out) ? out : stdout, "%s", sbuf); } - if (wrp && l != '\n') - fprintf((argc == 2) ? out : stdout, "\n"); } if (in) fclose(in); diff --git a/output.c b/output.c index d5c1a3a..b48713e 100644 --- a/output.c +++ b/output.c @@ -8,6 +8,7 @@ printw(FILE *fp, unsigned char *s, int slen) static int col; int c; + c = '\n'; while (slen--) { fputc((c = *s++), fp); ++col; From 4e8bb86ddd7c38b8248c1b19e66d058405cfc974 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 28 May 2025 17:55:13 +0200 Subject: [PATCH 2/2] change Makefile to be stricter -Og enables better detection of bugs that would only occur in optimized compilations and allows for slightly lesser than -O1 optimization that does not interfere with debugging. --- Makefile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index ff2a3f4..b66cfcc 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,14 @@ -CFLAGS = -g -Wall -Wextra -Werror +CC = gcc +CFLAGS = -Og -Wall -Wextra -Werror objects = main.o input.o output.o encode.o b64 : $(objects) - cc -o b64 $(objects) + $(CC) -o b64 $(objects) main.o input.o : input.h -main.o output.o : output.h main.o encode.o : encode.h +main.o output.o : output.h .PHONY : clean clean :