I have some C code performing a BASIC INPUT statement while reading from a file. The opening bit of this code is...
charbuffer[MAX_INPUT_LENGTH];inttype;//getthestreamintchannel=floor(evaluate_expression(statement->parms.generic.generic_parameter).number);FILE*fp=handle_for_channel(channel);if(fp==NULL){handle_error(ern_FILE_NOT_OPEN,"Attempt to INPUT from a file that has not been OPENed");//andmakesurewecanreaditif(!channel_is_readable(channel)){handle_error(ern_FILE_NOT_INPUT,"Attempt to INPUT from a file that is read-only");}}//readonelinefromthefile,ifit'sempty,warnandreturnif(fgets(buffer,sizeofbuffer,fp)==NULL){handle_error(ern_OUT_OF_DATA,"Reached the end-of-file while performing INPUT");return;}longlen=strlen(buffer);
The question has to do with the fgets line, which in cppcheck is returning:
Either the condition 'fp==NULL' is redundant or there is possible null pointer dereference: fp
But, to my not very expert eye, I am not comparing fp to null. Am I misinterpreting what is going on here, or is this a spurious warning?
Complete code here, it is small and should easily make on practically anything:
First post here, please be gentle!
I have some C code performing a BASIC
INPUT
statement while reading from a file. The opening bit of this code is...The question has to do with the fgets line, which in cppcheck is returning:
But, to my not very expert eye, I am not comparing fp to null. Am I misinterpreting what is going on here, or is this a spurious warning?
Complete code here, it is small and should easily make on practically anything:
https://github.com/maurymarkowitz/RetroBASIC
Does
handle_error()
return or abort? If it returns,fp
can be used even if it isNULL
.handle_error MAY abort, but will not if a TRAP is set, which it often is during I/O.
But the warning is not related to that I don't think?
Ohhh, wait now I see what you mean. It's not complaining about fp==NULL on this line, it's saying the one above is the issue. Now I get it!