Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is printf()'s string width safe with unterminated strings?

Is the following well defined?

const char not_a_c_string[] = { 'h', 'e', 'l', 'l', 'o' };
printf( "%.5s", (const char*) not_a_c_string );

This is a question about the specific form "%.5s", and not an how to print a possibly not NUL-terminated string? as this question has already been answered here where the "%.*s" construct is suggested.

like image 225
YSC Avatar asked Jan 12 '16 16:01

YSC


1 Answers

First of all, I believe, you meant to ask about the precision, not the field width. So, your example is to look like

 printf( "%.5s", (const char*) not_a_c_string );  //precision

instead of

 printf( "%5s", (const char*) not_a_c_string );   //field width.

Considering the above approach, no, it will not be UB in your example.

To quote the C11 standard, chapter §7.21.6.1, The fprintf function, paragraph 8, (emphasis mine)

s               If no l length modifier is present, the argument shall be a pointer to the initial element of an array of character type.(280) Characters from the array are written up to (but not including) the terminating null character. If the precision is specified, no more than that many bytes are written. If the precision is not specified or is greater than the size of the array, the array shall contain a null character.

So, you need to have a null delimited array (string) only if you're either

  • missing the precision
  • supplied precision is > the size of the supplied char array.

In your case, the mentioned precision (5) is not greater that the size of the array (also 5). So, It's fine.


FWIW, if the example remains

 printf( "%5s", (const char*) not_a_c_string );

then it will be UB, as you'll be missing precision there.

like image 193
Sourav Ghosh Avatar answered Nov 07 '22 01:11

Sourav Ghosh