[MacPorts] #69384: gklib @20230327: error: 'lnlen' may be used uninitialized in this function (was: gklib fails on 10.5 PPC)
MacPorts
noreply at macports.org
Fri May 3 11:58:50 UTC 2024
#69384: gklib @20230327: error: 'lnlen' may be used uninitialized in this function
-----------------------+---------------------------------------
Reporter: rmottola | Owner: catap
Type: defect | Status: assigned
Priority: Normal | Milestone:
Component: ports | Version:
Resolution: | Keywords: tiger leopard snowleopard
Port: gklib |
-----------------------+---------------------------------------
Changes (by ryandesign):
* keywords: leopard gcc => tiger leopard snowleopard
Comment:
Please attach the main.log file.
I am not able to reproduce the problem on macOS 12 building using Xcode
clang or MacPorts gcc 13. I can't test gcc 7 because it's too old for this
OS.
Replying to [ticket:69384 rmottola]:
> {{{
> io.c: In function 'gk_readfile':
> io.c:113:24: error: 'lnlen' may be used uninitialized in this function
[-Werror=maybe-uninitialized]
> if (*lineptr == NULL || *n == 0) {
> ~~~~~~~~~~~~~~~~~^~~~~~~~~~
> }}}
`gk_readfile` uses `lnlen` only once, at the end of this chunk:
https://github.com/KarypisLab/GKlib/blob/8bd6bad750b2b0d90800c632cf18e8ee93ad72d7/io.c#L147-L159
{{{#!c
char **gk_readfile(char *fname, size_t *r_nlines)
{
size_t lnlen, nlines=0;
char *line=NULL, **lines=NULL;
FILE *fpin;
gk_getfilestats(fname, &nlines, NULL, NULL, NULL);
if (nlines > 0) {
lines = (char **)gk_malloc(nlines*sizeof(char *), "gk_readfile:
lines");
fpin = gk_fopen(fname, "r", "gk_readfile");
nlines = 0;
while (gk_getline(&line, &lnlen, fpin) != -1) {
}}}
As we see at the top of the function, at this point `line` is a `NULL`
pointer and `lnlen` is uninitialized.
`gk_getline` in turn does this:
https://github.com/KarypisLab/GKlib/blob/8bd6bad750b2b0d90800c632cf18e8ee93ad72d7/io.c#L101C1-L116
{{{#!c
ssize_t gk_getline(char **lineptr, size_t *n, FILE *stream)
{
#ifdef HAVE_GETLINE
return getline(lineptr, n, stream);
#else
size_t i;
int ch;
if (feof(stream))
return -1;
/* Initial memory allocation if *lineptr is NULL */
if (*lineptr == NULL || *n == 0) {
*n = 1024;
*lineptr = gk_malloc((*n)*sizeof(char), "gk_getline: lineptr");
}
}}}
The way I read this, in this call, `*lineptr` (aka `line`) is `NULL`, so
the second part of the condition checking if `*n` (aka `lnlen` which is
uninitialized) is `0` is not evaluated, so I don't think the variable is
being used uninitialized. You could still report the problem to the
developers; it would be simple to just initialize the variable anyway.
`HAVE_GETLINE` is true on Mac OS X 10.7 or later, so this problem only
affects Mac OS X 10.6 and earlier, which is probably why not more people
have reported it.
I tried removing these lines from GKlibSystem.cmake (because I could find
no way to override this result on the command line):
{{{
check_function_exists(getline HAVE_GETLINE)
if(HAVE_GETLINE)
set(GKlib_COPTIONS "${GKlib_COPTIONS} -DHAVE_GETLINE")
endif(HAVE_GETLINE)
}}}
Even so, I did not see a build error with Xcode clang or MacPorts gcc 13.
Maybe this warning was improved in later GCC versions to be more accurate.
The build system sets `-Werror` only when the compiler is GCC. I submitted
a PR to stop doing that:
https://github.com/KarypisLab/GKlib/pull/36
--
Ticket URL: <https://trac.macports.org/ticket/69384#comment:5>
MacPorts <https://www.macports.org/>
Ports system for macOS
More information about the macports-tickets
mailing list