I have a printf
-like function that can handle %s
(char *
) and %ls
(wchar_t *
) conversions. Everything works fine if I pass the right argument for the right conv specifier.
But if I pass a char *
to my function when it expects a wchar_t *
, it may segfault (Null-terminating byte being located at the second byte of a wchar_t
for instance). Since I access this argument through va_arg()
I can't be sure of the type.
If I consider that this char
array is always NUL-terminated, I could check byte after byte to correctly handle the NUL-terminating char and stop memory access after it. But then I wouldn't be able to handle wchar_t
legit values like this :
0b XXXXXXXX XXXXXXXX 00000000 XXXXXXXX
I'm already using the __attribute__
printf GNU C extension. But I this function may be used by a python programm through ctypes
, so format/type checking at compiling may not be enough.
It there a way to perform such checking at runtime in my C function?
(NB : "There is no such way" may be the answer, but I'm still asking in order to be completely sure.)