c - What is the correct type to use for declaring a metavariable that possibly could match either variables or members in a struct? -
what correct type use declaring metavariable possibly match either variables or members in struct?
take instance following example source code:
#include <stdio.h> #include <stdlib.h> struct some_struct { int i; char *s; }; void test(void) { struct some_struct *ptr; char *s; s = malloc(100); ptr = malloc(sizeof(struct some_struct)); ptr->s = malloc(100); puts("done"); }
with following semantic patch:
@@ identifier ptr; //idexpression ptr; //expression ptr; expression e; @@ ptr = malloc(e); +if (ptr == null) + return;
the ptr->s
allocation not matched unless expression ptr
used. use expression
seems bit broadly me. correct , way it?
in general, want catch lvalue pointer - since you're matching places expression assigned value malloc
, plain expression job fine (since non-pointer or non-lvalue should make compiler complain).
the problem you're going have if expression has sideeffects, eg:
struct some_struct *a[10]; int = 0; a[i++] = malloc(sizeof(struct some_struct));
Comments
Post a Comment