Re: memcmp() checker: memory access errors




On Mon, 17 Dec 2006, kolmogolov@xxxxxxxxx wrote:

/*
Hi, I have removed things irrelevant to reproducing the problem.
What's wrong with my code? Thanks in advance for any hint!
1166425362
*/

I see four bugs, two of which /might/ cause your problem. First, you malloc 'data' and never free it; second, and more importantly,
you try to 'fread' out of a file that was opened as text, not binary.
To fix the second bug, use "rb" instead of "r" as the second argument
to 'fopen'. Does the problem go away?

Any time you're dealing with bits and bytes, you should be using
files opened in binary mode, not text mode.

Third, the line
assert ( 1 == fread(data, probe_len, 1, fp) );
should probably be changed stylistically to
assert ( probe_len == fread(data, 1, probe_len, fp) );

and /definitely/ needs to be changed to

rc = fread(data, 1, probe_len, fp);
assert(rc == probe_len);

so that it will continue to work when NDEBUG is #defined (e.g., when
you release the product). This is probably the big bug.

Never put anything inside 'assert' that you want to be evaluated.
The same goes for any in-house assertion-checking macros or functions;
it'll make life easier for the programmers who come after you.

-Arthur


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

int probe_img(FILE *fp)
{
unsigned char *data;
unsigned char KDF_header[4]={0x01, 0x03, 0x13, 0x5E};
int probe_len = 256;
int type=-1;

data = malloc( probe_len );
assert ( NULL != data );
assert ( 1 == fread(data, probe_len, 1, fp) );

if ( !memcmp(data, KDF_header, 4) )
{
type = 3;
}

return type;
}

int main(void)
{
FILE *fp = fopen("sample.kdf", "r");
assert ( NULL != fp);
printf("type=%d\n", probe_img(fp) );
return 0;
}

/*
$ echo $CHECKEROPTS
[big snip]
.



Relevant Pages

  • Re: memcmp() checker: memory access errors
    ... unsigned char *data; ... int probe_len = 256; ... data = malloc(probe_len); ... P.S. Dangerous usage of `assert' is addressed in the reply by Richard Bos. ...
    (comp.lang.c)
  • Re: Cipher Lab / Syntech
    ... this is not Google Groups. ... The "simple" library without malloc is needed because I was told ... void foo { ... int dprintf { ...
    (comp.lang.c)
  • Re: why still use C?
    ... I was talking about the malloc expression, ... >>the assignment expression. ... >>enum parameter is not an error in C, but in my coding style it's a mistake). ... I took it you meant "int where an enum" is expected, ...
    (comp.lang.c)
  • Re: malloc + 4??
    ... >>information into the malloc is solid. ... The variable inSize is a plain int and has ... > the loop will never terminate. ... > yet also return the special marker value EOF. ...
    (comp.lang.c)
  • Re: Unknown function
    ... How can the function call of 'malloc' work at all if it is unknown? ... I thought that each function that is unknown to the compiler at a specific ... returns an int. ... problem since malloc actually takes size_t and returns void *. ...
    (comp.lang.c)