Fix warning: ‘startpos’ may be used uninitialized in function ‘results_differ’

Started by Noname6 months ago7 messageshackers
Jump to latest
#1Noname
m.litsarev@postgrespro.ru

Hi, hackers!

I found warning while compiling the code (introduced in
5720ae01436480c1dcdd34ece0c3d83b00844bca)
with the folloing message

postgres/build/../src/test/regress/pg_regress.c: In function
‘results_differ’:
postgres/build/../src/test/regress/pg_regress.c:1577:17: warning:
‘startpos’ may be used uninitialized in this function
[-Wmaybe-uninitialized]
1577 | fseek(difffile, startpos, SEEK_SET);

gcc (Ubuntu 11.5.0-10ubuntu1~22~ppa1) 11.5.0
ldd (Ubuntu GLIBC 2.35-0ubuntu3.13) 2.35

./configure --enable-debug --enable-cassert --enable-depend
--enable-tap-tests --enable-nls --with-perl --with-icu --with-libxml
--with-libxslt --with-gssapi --with-openssl --with-zstd --with-lz4
--with-ldap --with-python --prefix=$PGINSTDIR --with-pgport=$PGPORT

CFLAGS="-ggdb -Og -g3 -fno-omit-frame-pointer -fvisibility=default
-Wno-clobbered"

It definitely seems false-positive from semantical point of view, but to
keep compiler silent I attached a small patch which fixes it.

Respectfully,
Mikhail Litsarev, PostgresPro

#2Noname
m.litsarev@postgrespro.ru
In reply to: Noname (#1)
Re: Fix warning: ‘startpos’ may be used uninitialized in function ‘results_differ’

The patch attached

Attachments:

v1-0001-Fix-warning-startpos-may-be-used-uninitialized-in.patchtext/x-diff; name=v1-0001-Fix-warning-startpos-may-be-used-uninitialized-in.patchDownload+39-41
#3Ewan Young
kdbase.hack@gmail.com
In reply to: Noname (#2)
Re: Fix warning: ‘startpos’ may be used uninitialized in function ‘results_differ’

Hi Mikhail,

On Thu, Jun 11, 2026 at 11:10 PM <m.litsarev@postgrespro.ru> wrote:

The patch attached

Thanks for the report and patch.

I looked at the data flow and agree this is a false positive. startpos is
assigned at the ftell() call inside the first if (difffile) block, and the
only way to reach the use at fseek() with difffile != NULL is for that same
block to have executed (since difffile is reassigned by the fopen(..., "r")
at the end of it).

I should note I couldn't reproduce the warning here: with gcc 12.2 it stays
silent at every optimization level, including -Og.

The fix looks correct to me. The raw diff looks large, but it's almost entirely
re-indentation — git diff -w shows the actual change is just moving the
braces to nest the read block inside the first one.

For the record, the minimal alternative would be long startpos = 0; at the
declaration, which is a one-line diff. I'd lean toward your version, but
I'll leave the final call to a committer.

Regards,
Ewan

#4Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Ewan Young (#3)
Re: Fix warning: ‘startpos’ may be used uninitialized in function ‘results_differ’

On 12/06/2026 04:53, Ewan Young wrote:

On Thu, Jun 11, 2026 at 11:10 PM <m.litsarev@postgrespro.ru> wrote:

The patch attached

Thanks for the report and patch.

I looked at the data flow and agree this is a false positive. startpos is
assigned at the ftell() call inside the first if (difffile) block, and the
only way to reach the use at fseek() with difffile != NULL is for that same
block to have executed (since difffile is reassigned by the fopen(..., "r")
at the end of it).

I should note I couldn't reproduce the warning here: with gcc 12.2 it stays
silent at every optimization level, including -Og.

The fix looks correct to me. The raw diff looks large, but it's almost entirely
re-indentation — git diff -w shows the actual change is just moving the
braces to nest the read block inside the first one.

For the record, the minimal alternative would be long startpos = 0; at the
declaration, which is a one-line diff. I'd lean toward your version, but
I'll leave the final call to a committer.

LGTM as far as the patch goes.

The function feels too nonchalant about errors while opening the file.
The diff output file was created earlier in open_result_files() already,
so it really should exist. I propose the attached, to bail out if the
fopen() fails. (As the original patch, this looks a lot smaller with
"diff -w")

- Heikki

Attachments:

v2-0001-Silence-uninitialized-variable-warning-with-some-.patchtext/x-patch; charset=UTF-8; name=v2-0001-Silence-uninitialized-variable-warning-with-some-.patchDownload+24-23
#5Noname
m.litsarev@postgrespro.ru
In reply to: Heikki Linnakangas (#4)
Re: Fix warning: ‘startpos’ may be used uninitialized in function ‘results_differ’

...I propose the attached, to bail out if the fopen() fails. (As the
original patch, this looks a lot smaller with "diff -w")

I agree the v2 patch is even better.

Mikhail

#6Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Noname (#5)
Re: Fix warning: ‘startpos’ may be used uninitialized in function ‘results_differ’

On 16/06/2026 18:22, m.litsarev@postgrespro.ru wrote:

...I propose the attached, to bail out if the fopen() fails. (As the
original patch, this looks a lot smaller with "diff -w")

I agree the v2 patch is even better.

Committed, thanks!

- Heikki

#7Imran Zaheer
imran.zhir@gmail.com
In reply to: Heikki Linnakangas (#6)
Re: Fix warning: ‘startpos’ may be used uninitialized in function ‘results_differ’

Hi

I also reported [1]/messages/by-id/CA+UBfa=H9RQdZ8Tof+SF7gMC-FTrsJ_w=Vnxs4OiBAaB_go+tQ@mail.gmail.com<span class="sewitjdq8nk9iqz"></span> some similar warnings emitted from the old gcc
some time ago. Do you think this should be worth getting fixed?

Thanks
Imran Zaheer

[1]: /messages/by-id/CA+UBfa=H9RQdZ8Tof+SF7gMC-FTrsJ_w=Vnxs4OiBAaB_go+tQ@mail.gmail.com<span class="sewitjdq8nk9iqz"></span>
class="sewitjdq8nk9iqz"></span>

Show quoted text

On Wed, Jun 17, 2026 at 11:03 AM Heikki Linnakangas <hlinnaka@iki.fi> wrote:

On 16/06/2026 18:22, m.litsarev@postgrespro.ru wrote:

...I propose the attached, to bail out if the fopen() fails. (As the
original patch, this looks a lot smaller with "diff -w")

I agree the v2 patch is even better.

Committed, thanks!

- Heikki