What is an "invalid conversion specification"?

According to C11 , chapter §7.21.6.1, P9

If the conversion specification is not valid, the behavior is undefined. 282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

Until that time, I realized that for

  char str [] = "Sourav"; 
  • Operator of type printf("%S", str); belongs to the first sentence, CS does not exist as %S (UPPERCASE)
  • Type statement printf("%d", str); refers to the second sentence (mismatch between CS and argument type, but %d not an invalid CS) anyway

until otherwise reported in a recent comment thread.

I understand what is wrong? Can the second operator also be classified as an "invalid" (PS-not "incorrect") conversion specifier?


Update: the response and the comment stream are deleted, here the binding for users <10K.

+4
c language-lawyer printf
source share
3 answers

The "validity" of the conversion specification is defined in standard paragraphs above the one you specified:

7.21.6.1 - p4 to p8

Each conversion specification is entered with a% character. After%, the following sequences appear: ...

Symbols of flags and their meanings: ...

Conversion qualifiers and their meanings: ...

This means that any conversion specification consisting of elements in the above lists is valid, all others do not comply with the standard. Therefore, in the paragraph of your code, two reasons for UB are mentioned. One of them is a specification that does not correspond to the grammar, and the other is a mismatch of specification and type.

The comment you referenced seems to be used "invalid" in normal mode. That is, both uses of the conversion specifications are "invalid" because they lead to UB. But only the first is "invalid" from the point of view of a language attorney.

+9
source share

To support my understanding (and probably to reasonify the understanding in first place), let me add my two cents.

For a minute, see footnote 282 as indicated in the quote. It says:

See "Future Directions of the Library (7.31.11).

and in §7.31.11

Lowercase letters can be added to conversion specifiers and length modifiers in fprintf and fscanf . Other characters may be used in extensions.

Which does not mention the relationship between CS and its argument (if any). Thus, the "validity" of the CS is independent of the argument.

Now saying a couple more pointers

  • Point 1: pay attention to the mention of the phrase "conversion specification", not the conversion specifier in the quote. According to the chapter of §7.21.6.1 / P4,

    Each conversion specification is entered with a % character. After % follows the following appear in sequence:

    • Zero or more flags [...]

    • Additional minimum field width [...]

    • Optional accuracy [...]

    • Additional length modifier [...]

    • Conversion specifier character [...]

    and we have final listings for all the items mentioned in

    • P5, field width and accuracy
    • P6 flags
    • P7 length modifier
    • P8, conversion specifier

    Thus, there is a connection (or should be) with the argument provided to determine the "validity" of the conversion specification.

    In addition to this understanding, borrowing words from the commentary of Ajay Brahmaksatriya

    “I think the current word here is“ consistent. ”The first sentence says that if there is a qualifier in the string that is not valid. If not, then each qualifier is matched with the corresponding argument. Then the second statement says the type match ..... I think the second example does not belong to the first category, because "the corresponding" is not used "

  • Point 2 :: . On the other hand, the specification is quite distinctly different from the “mismatch” between the CS and the corresponding argument type provided. So this is a completely different case.

Now, for example, in the case where both cases are combined, it is difficult to say which condition causes the UB, but it is UB, for several reasons.

Example:

  printf("%D", str); 

after the question.

+1
source share

Footnote 282 indicates the future directions of the library C11 7.31.11p1 :

Lowercase letters can be added to conversion specifiers and length modifiers in fprintf and fscanf. Other characters may be used in extensions.

therefore, he also hints that invalid conversion specifiers mean those conversion specifications that are not on the list, of which lowercase letters can be used by a future version of C; and extensions are free to use other letters.


And although non-normative, C11 Appendix J.2. contains the following:

  • An invalid conversion specification was found in the format for one of the formatted I / O functions or the strftime or wcsftime (7.21.6.1, 7.21.6.2, 7.27.3.5, 7.29.2.1, 7.29) .. 2.2, 7.29.5.1).

i.e. an invalid conversion specification for *printf is associated with an invalid conversion specification before strftime - which does not accept variable arguments, and cannot be invalidated due to a mismatch between the conversion specification and the corresponding argument;

This can be contrasted with

  • There are not enough arguments for the format when calling one of the formatted input / output functions or the argument does not have the corresponding type (7.21.6.1, 7.21.6.2, 7.29.2.1, 7.29.2.2).

which discusses the mismatch between arguments and conversion qualifiers without mentioning the word invalid.

+1
source share

All Articles