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.
This commit is contained in:
Eric 2025-05-28 17:31:07 +02:00
parent fa5095133a
commit 733a013ee7
2 changed files with 18 additions and 14 deletions

31
main.c
View file

@ -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);