c - converting sprintf to snprintf using MACRO -


we have code following:

#define maxint  1000000000  #define sprintf(a, b, c)    ((sizeof (a) > 8) ? snprintf(a, sizeof (a), b, c) : snprintf(a, maxint, b, c))                             // avoids buffer overrun static buffers of size > 8 else behaves default sprintf()                             // note max. size of char pointer 8, if sizeof (a) > 8, means static array 

i remember developer asked convert sprintf() safer version snprintf() , did change shown above.

i understand above macro avoids buffer corruption static buffers of size > 8 , other i.e. dynamic buffers , static buffers of size <=8 behaves normal sprintf() assuming string being copied not greater "maxint". correct?

for ex. if string being copied less dest buffer or greater dest buffer, statement/above macro behave correctly either snprintf(a, sizeof(a), b, c) or normal default sprintf() - kind of ok behaviour time being. assume never fill dest buffer maxint (too big , src string never big) amount of characters?

the macro has conditional 'a > 8' check if of type char* or char[]. if char*, there no way of knowing how memory allocated , how can write it. if char[], know how memory reserved it. char[], can use snprintf , thereby safer ensuring never overrun. char*, cannot offer same. code assuming never want write more maxint.

if writing macro, have changed snprintf(a, maxint, b, c) sprintf(a, b, c) make clearer unable offer guarantees in scenario.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -