Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange type mismatch in malloc #29

Open
jameshfisher opened this issue Apr 24, 2016 · 2 comments
Open

Strange type mismatch in malloc #29

jameshfisher opened this issue Apr 24, 2016 · 2 comments

Comments

@jameshfisher
Copy link

Take the following program:

#include <stdlib.h> /* malloc */

void foo()
//@ requires true;
//@ ensures true;
{
  unsigned int count = 3;
  malloc(sizeof(unsigned char) * count);
}

After applying my patch in #28 and running VeriFast, i get the error:

foo.c(8,34-39): Type mismatch. Actual: uintptr_t. Expected: int.

The IDE highlights count as the source of the type error. I don't know where the int expectation is coming from.

I don't understand why, if I pull out the expression into its own variable, VeriFast passes:

#include <stdlib.h> /* malloc */
#include <stddef.h> /* size_t */

void foo()
//@ requires true;
//@ ensures true;
{
  unsigned int count = 3;
  size_t size = sizeof(unsigned char) * count;
  malloc(size);
}
@btj
Copy link
Member

btj commented Apr 25, 2016

VeriFast treats expressions of the form malloc(sizeof(T) * E) or malloc(E * sizeof(T)) specially, to generate the appropriate typed array chunk (like ints) declared in prelude.h. Currently, it typechecks E against type int. See https://github.com/verifast/verifast/blob/master/src/verify_expr.ml#L1818.

I guess it should instead typecheck E against type size_t.

@jameshfisher
Copy link
Author

Ah, that would explain it. I'm not sure what E should typecheck as. I think there are many valid types. My intuition is that a size_t multiplied by any natural number should yield another size_t, so E just has to be integral and non-negative. But this depends on C's type conversion rules, which I'm not very familiar with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants