dimkr / loksh

A Linux port of OpenBSD's ksh
115 stars 6 forks source link

compiler warning when building from source #35

Open mm1ke opened 1 week ago

mm1ke commented 1 week ago

Hi,

I'm trying to update loksh on gentoo to the latest version. However i'm seeing compiler warnings when building it:

[35/35] x86_64-pc-linux-gnu-gcc  -o ksh ksh.p/alloc.c.o ksh.p/c_ksh.c.o ksh.p/c_sh.c.o ksh.p/c_test.c.o ksh.p/c_ulimit.c.o ksh.p/edit.c.o ksh.p/emacs.c.o ksh.p/eval.c.o ksh.p/exec.c.o ksh.p/expr.c.o ksh.p/history.c.o ksh.p/io.c.o ksh.p/jobs.c.o ksh.p/lex.c.o ksh.p/mail.c.o ksh.p/main.c.o ksh.p/misc.c.o ksh.p/path.c.o ksh.p/shf.c.o ksh.p/syn.c.o ksh.p/table.c.o ksh.p/trap.c.o ksh.p/tree.c.o ksh.p/tty.c.o ksh.p/var.c.o ksh.p/version.c.o ksh.p/vi.c.o -flto -Wl,--as-needed -Wl,--no-undefined -O2 -pipe -march=znver4 -mshstk --param=l1-cache-line-size=64 --param=l1-cache-size=32 --param=l2-cache-size=1024 -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing -Wl,-O1 -Wl,--as-needed -Wl,-z,pack-relative-relocs -fuse-ld=mold -Wl,--start-group subprojects/lolibc/liblolibc.a /usr/lib64/libncurses.so /usr/lib64/libtinfo.so -Wl,--end-group
In function ‘afree’,
    inlined from ‘shf_sclose’ at ../loksh-7.5/shf.c:271:3,
    inlined from ‘shf_snprintf’ at ../loksh-7.5/shf.c:697:2:
../loksh-7.5/alloc.c:127:9: warning: ‘free’ called on unallocated object ‘shf’ [-Wfree-nonheap-object]
  127 |         free(l);
      |         ^
../loksh-7.5/shf.c: In function ‘shf_snprintf’:
../loksh-7.5/shf.c:685:20: note: declared here
  685 |         struct shf shf;
      |                    ^
In function ‘afree’,
    inlined from ‘shf_sclose’ at ../loksh-7.5/shf.c:271:3,
    inlined from ‘shf_smprintf.constprop’ at ../loksh-7.5/shf.c:711:9:
../loksh-7.5/alloc.c:127:9: warning: ‘free’ called on unallocated object ‘shf’ [-Wfree-nonheap-object]
  127 |         free(l);
      |         ^
../loksh-7.5/shf.c: In function ‘shf_smprintf.constprop’:
../loksh-7.5/shf.c:704:20: note: declared here
  704 |         struct shf shf;
      |                    ^
In function ‘afree’,
    inlined from ‘shf_sclose’ at ../loksh-7.5/shf.c:271:3,
    inlined from ‘snptreef’ at ../loksh-7.5/tree.c:358:10:
../loksh-7.5/alloc.c:127:9: warning: ‘free’ called on unallocated object ‘shf’ [-Wfree-nonheap-object]
  127 |         free(l);
      |         ^
../loksh-7.5/tree.c: In function ‘snptreef’:
../loksh-7.5/tree.c:350:14: note: declared here
  350 |   struct shf shf;
      |              ^
In function ‘afree’,
    inlined from ‘shf_sclose’ at ../loksh-7.5/shf.c:271:3,
    inlined from ‘wdstrip’ at ../loksh-7.5/tree.c:561:11,
    inlined from ‘function_body’ at ../loksh-7.5/syn.c:538:10:
../loksh-7.5/alloc.c:127:9: warning: ‘free’ called on unallocated object ‘shf’ [-Wfree-nonheap-object]
  127 |         free(l);
      |         ^
../loksh-7.5/tree.c: In function ‘function_body’:
../loksh-7.5/tree.c:548:20: note: declared here
  548 |         struct shf shf;
      |                    ^

I'm using gcc-14 as compiler. Is this something which could be fixed?

dimkr commented 1 week ago

I wonder if OpenBSD has these warnings too 🤔

dimkr commented 1 week ago

This could be a false positive, maybe GCC doesn't understand that the free() is conditional:

int
shf_snprintf(char *buf, int bsize, const char *fmt, ...)
{
       [...]

        shf_sopen(buf, bsize, SHF_WR, &shf);    <== passes &shf, not NULL

       [...]
struct shf *
shf_sopen(char *buf, int bsize, int sflags, struct shf *shf)
{       
        [...]

        if (!shf) {     <== this is false, so SHF_ALLOCS is not set
                shf = alloc(sizeof(struct shf), ATEMP);
                sflags |= SHF_ALLOCS;
        }

        [...]
        if (shf->flags & SHF_ALLOCS)    <== therefore this is false and free() is not called
                afree(shf, shf->areap);