View Single Post
Old 2014-08-16, 15:19   #1
Explorer09
 
May 2014

1000012 Posts
Default Opinion about Prime95 compile warnings and -Wno-unused-result

I tried to compile mprime 28.5 on Ubuntu (Linux) 14.04 for my own use. However GCC in Ubuntu's default setting generates many compiler warnings. Although I read the makefile which suggests they can be suppressed by -Wno-unused-result compiler flag, it still makes me curious to know what they're about.

And I found out there are indeed code that I can consider bad practice, and the compiler did the right thing by generating warnings.

One example:
Code:
../ecm.c: In function ‘ecm_QA’:
../ecm.c:3568:3: warning: ignoring return value of ‘fscanf’, declared with attribute warn_unused_result [-Wunused-result]
   (void) fscanf (fd, "%lf,%lu,%lu,%ld,%lf,%lu,%lu,%lu,%s\n",
   ^
Code:
/* Read a line from the file */
    n = 0;
    (void) fscanf (fd, "%lf,%lu,%lu,%ld,%lf,%lu,%lu,%lu,%s\n",
        &k, &b, &n, &c, &sigma, &B1, &B2_start, &B2_end,
        fac_str);
    if (n == 0) break;
......
The code is supposed to parse a file (named 'qa_ecm'), but does not check whether the file can EOF at the middle of the line. The return code of fscanf can be used to do this, but here it is intentionally ignored.

I think it will be better to rewrite it to something like this:
(EOF at the middle of the line usually means file is corrupt, so return an error that way)
Code:
int rc;
rc = fscanf (fd, "%lf,%lu,%lu,%ld,%lf,%lu,%lu,%lu,%s\n",
    &k, &b, &n, &c, &sigma, &B1, &B2_start, &B2_end,
    fac_str);
if (rc < 9) { // Note: (rc != EOF) won't work!
    OutputStr (thread_num, "File corrupt.\n");
    return (STOP_FILE_IO_ERROR);
}
Fixing all of these can take a lot of time, and I have little to help. But it's a bad idea to suppress there warnings - just keep them on (-Wunused-result) and we can easily spot bugs through them.
Explorer09 is offline   Reply With Quote