xlazom00

0002-libc-scanf-do-not-count-suppressed-conversions.patch

Oct 5th, 2026
26,776
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. [libc] scanf: do not count suppressed conversions in the return value
  2.  
  3. scanf_main() incremented the result for every successful conversion except %n,
  4. including assignment-suppressed ones ("%*d") and "%%". C requires the number of
  5. input items *assigned*. Example: sscanf("12 34", "%*d%d", &x) returned 2
  6. instead of 1; sscanf("420jpeg", "%*d%*[pP]%d", &x) returned 1 without writing x
  7. (breaks x264's y4m colorspace/bit depth parsing). Still present in llvm main
  8. as of 2026-10.
  9.  
  10. diff --git a/libc/src/stdio/scanf_core/scanf_main.h b/libc/src/stdio/scanf_core/scanf_main.h
  11. index f975d85..e1eb5fb 100644
  12. --- a/libc/src/stdio/scanf_core/scanf_main.h
  13. +++ b/libc/src/stdio/scanf_core/scanf_main.h
  14. @@ -32,9 +32,10 @@ int scanf_main(Reader<T> *reader, const char *__restrict str,
  15. cur_section = parser.get_next_section()) {
  16. if (cur_section.has_conv) {
  17. ret_val = convert(reader, cur_section);
  18. - // The %n (current position) conversion doesn't increment the number of
  19. - // assignments.
  20. - if (cur_section.conv_name != 'n')
  21. + // Only assigned items count: not %n (current position), not %% and not
  22. + // conversions with assignment suppression ('*', the NO_WRITE flag).
  23. + if (cur_section.conv_name != 'n' && cur_section.conv_name != '%' &&
  24. + (cur_section.flags & FormatFlags::NO_WRITE) == 0)
  25. conversions += ret_val == READ_OK ? 1 : 0;
  26. } else {
  27. ret_val = raw_match(reader, cur_section.raw_string);
  28.  
Advertisement