Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a scanf width specifier of 0?

Tags:

c

scanf

How to use a scanf width specifier of 0?
1) unrestricted width (as seen with cywin gcc version 4.5.3)
2) UB
3) something else?

My application (not shown) dynamically forms the width specifier as part of a larger format string for scanf(). Rarely it would create a "%0s" in the middle of the format string. In this context, the destination string for that %0s has just 1 byte of room for scanf() to store a \0 which with behavior #1 above causes problems.

Note: The following test cases use constant formats.

#include <memory.h>
#include <stdio.h>

void scanf_test(const char *Src, const char *Format) {
  char Dest[10];
  int NumFields;
  memset(Dest, '\0', sizeof(Dest)-1);
  NumFields = sscanf(Src, Format, Dest);
  printf("scanf:%d Src:'%s' Format:'%s' Dest:'%s'\n", NumFields, Src, Format, Dest);
}

int main(int argc, char *argv[]) {
  scanf_test("1234" , "%s");
  scanf_test("1234" , "%2s");
  scanf_test("1234" , "%1s");
  scanf_test("1234" , "%0s");
  return 0;
}

Output:

scanf:1 Src:'1234' Format:'%s' Dest:'1234'  
scanf:1 Src:'1234' Format:'%2s' Dest:'12'  
scanf:1 Src:'1234' Format:'%1s' Dest:'1'  
scanf:1 Src:'1234' Format:'%0s' Dest:'1234' 

My question is about the last line. It seems that a 0 width results in no width limitation rather than a width of 0. If this is correct behavior or UB, I'll have to approach the zero width situation another way or are there other scanf() formats to consider?

like image 688
chux - Reinstate Monica Avatar asked May 29 '13 16:05

chux - Reinstate Monica


2 Answers

The maximum field width specifier must be non-zero. C99, 7.19.6.2:

The format shall be a multibyte character sequence, beginning and ending in its initial shift state. The format is composed of zero or more directives: one or more white-space characters, an ordinary multibyte character (neither % nor a white-space character), or a conversion specification. Each conversion specification is introduced by the character %. After the %, the following appear in sequence:
— An optional assignment-suppressing character *.
An optional nonzero decimal integer that specifies the maximum field width (in characters).
— An optional length modifier that specifies the size of the receiving object.
— A conversion specifier character that specifies the type of conversion to be applied.

So, if you use 0, the behavior is undefined.

like image 150
jxh Avatar answered Oct 17 '22 16:10

jxh


This came from 7.21.6.2 of n1570.pdf (C11 standard draft):

After the %, the following appear in sequence:

— An optional assignment-suppressing character *.

— An optional decimal integer greater than zero that specifies the maximum field width (in characters).

...

It's undefined behaviour, because the C standard states that your maximum field width must be greater than zero.

An input item is defined as the longest sequence of input characters which does not exceed any specified field width and ...

What is it you wish to achieve by reading a field of width 0 and assigning it as a string (empty string) into Dest? Which actual problem are you trying to solve? It seems more clear to just assign like *Dest = '\0';.

like image 4
autistic Avatar answered Oct 17 '22 18:10

autistic