c - macro to check if the return value of a function is being checked -


i have function

void *custom_get_value(objectptr)  

this function traditionally never used return null.it can return of following values

uint32_t int32_t  uint64_t  int64_t uint8_t 

since function never used return null have lot of code

       *(uint32_t*)custom_get_value(objectptr)                      or        *(uint64_t*)custom_get_value(objectptr)  

recently have decided modify behaviour of

void *custom_get_value(objectptr) in such way can return null.so occourances of above scenario (de-referencing specific types without checking return value) can result in segmentation fault. 

can use macro idendify places in code return value of

void *custom_get_value(objectptr) 

is not being checked.if yes how do that?

you can't write macro can track happens value after it's been returned, because data flow analysis beyond preprocessor's abilities. however, there useful technique designed situations such this, define function want check self-referential macro:

#define custom_get_value(...) (0, custom_get_value(__va_args__)) 

this example doesn't itself, showcases useful: when macro name appears part of own replacement list, nested occurrence of name "painted blue" , not expand second time. defining existing function name macro, can "wrap" (or "advise") existing invocations of function - presumably scattered across code in unknown number of places - actions, in addition retaining original invocation.

then have several options, e.g.:

#define custom_get_value(...) ({ \     _pragma("message \"custom_get_value called\""); \     custom_get_value(__va_args__); \ }) 

...if you're on gcc or clang, should list file , line number of every point custom_get_value called, @ compile-time. (both #pragma message , statement expression form nonstandard gcc extensions - if you're going use while cleaning old calls, perhaps doesn't matter? once calls checked can remove nonstandard code.)

or apply runtime message instead:

#define custom_get_value(...) \     custom_wrap(custom_get_value(__va_args__), __file__, __line__)  void * custom_wrap(void * val, char * f, int l) {     printf("custom_get_value called in %s on line %d\n", f, l); return val; } 

...which drive nuts printing message every time function gets called, @ least standard.

you wrap calls function renders resulting pointer guaranteed non-null once again, although that's not best way deal situation.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -