Checking runtime for pointers

I want to know how the scanf function is implemented. (Just for fun, of course) The number of arguments is variable, so it is certainly implemented by the macros va_list , va_arg .

It also issues some warnings when the number of arguments does not match the format string. This can be done by parsing the format string and comparing it with the number of arguments. No magic.

The only thing I do not see how this is implemented is type checking. When the type of the argument (data pointer) does not match the corresponding declaration in the format literature, scanf issues a warning. How to check the type of data the pointer points to?

Example:

 #include<stdio.h> int main() { char buffer1[32], buffer2[32]; int n; double x; scanf("%s %s %d",buffer1, buffer2, &x); // warning scanf("%s %s %d",buffer1, buffer2, &n); // ok } 

Output:

 warning: format '%d' expects argument of type 'int *', but argument 4 has type 'double *' [-Wformat] 

AFAIK C-library is not part of the C / Compiler language, so there is no language binding in <stdio.h> . I assume that the warning is generated by implementing scanf , and not by the compiler [?]. (Perhaps using #warning )

If I want to do something similar in some code, how do I know which data type the pointer points to?

Note I downloaded the source code of the GNU C library and looked at scanf.c . I cannot find my way through very complex code. There is a lot of #ifndef and calls other functions with strange names and structure ...

+4
source share
2 answers

This check is handled by the gcc compiler, in particular for the scanf / printf functions.

This is such a common mistake that it is worth adding special code for the compiler for these functions.

see here the GCC -WFormat flag: http://gcc.gnu.org/onlinedocs/gcc-3.4.4/gcc/Warning-Options.html

-Wformat: check the calls to printf and scanf, etc., to make sure that the arguments provided are of the type corresponding to the format string and that the conversions specified in the format string make sense. This includes standard functions, and others (see Function Attributes) in the printf, scanf, strftime and strfmon (X / Open extension, not C standard) families.

These checks are not performed by all compilers, so of course there is nothing to rely on.

With GCC, you can use the Function Attributes 'format' and 'format-arg' to tell the compiler to apply the same checks to your functions.

format (archetype, string-index, first-to-check) The format attribute indicates that the function accepts printf, scanf, strftime or strfmon style arguments that should be checked for format compliance. For example, an ad:

 extern int my_printf (void *my_object, const char *my_format, ...) __attribute__ ((format (printf, 2, 3))); 

... forces the compiler to check the arguments in my_printf calls for consistency with the printf my_format format string style argument.

+3
source

A warning is generated by the compiler. You declared x as double , so it knows that &x is double* . Then it scans the format string and sees that the format requires int* , so it warns.

+2
source

Source: https://habr.com/ru/post/1439819/


All Articles