[ET Trac] [Einstein Toolkit] #2106: Make ParseFile.c more robust
Einstein Toolkit
trac-noreply at einsteintoolkit.org
Mon Mar 12 12:15:13 CDT 2018
#2106: Make ParseFile.c more robust
-----------------------+----------------------------------------------------
Reporter: eschnett | Owner:
Type: defect | Status: reopened
Priority: unset | Milestone:
Component: Cactus | Version: development version
Resolution: | Keywords:
-----------------------+----------------------------------------------------
Comment (by rhaas):
Uff, I see. "usual arithmetic conversion" is really murky to me. So I try
to err on the side of not relying on it.
I think I would agree with this assessment if the conversion happened in
in the if statement. However the actual code is:
{{{
static char *ReadFile(FILE *file, unsigned long *filesize)
{
...
*filesize = ftell(file);
if (*filesize < 0)
{
// do stuff
}}}
so the result of ftell (a long int) is converted to unsigned long int when
it is assigned to *filesize. Since {{{filesize}}} is known to be "unsigned
long int" the optimization seems valid to me (namely an unsigned int
comparison with <0 will always fail).
The cast of -1 to {{{unsigned long int}}} that I added is just to avoid
the warning about comparing signed and unsigned values.
A test code:
{{{
void foo()
{
unsigned int bar = 1;
if(bar < 0) {
baz();
}
if(bar == -1) {
baz();
}
}
}}}
gives me two warnings on gcc 7.3.0 when using -Wextra (but not eg -Wall):
{{{
rhaas at ekohaes8:.../rhaas/tmp$ gcc -Wall -Wextra -c compare.c
compare.c: In function ‘foo’:
compare.c:4:10: warning: comparison of unsigned expression < 0 is always
false [-Wtype-limits]
if(bar < 0) {
^
compare.c:8:10: warning: comparison between signed and unsigned integer
expressions [-Wsign-compare]
if(bar == -1) {
^~
}}}
Note the first warning about the comparison always being false.
--
Ticket URL: <https://trac.einsteintoolkit.org/ticket/2106#comment:9>
Einstein Toolkit <http://einsteintoolkit.org>
The Einstein Toolkit
More information about the Trac
mailing list