This is an archive of the discontinued LLVM Phabricator instance.

[libc] Prevent printf index mode crashes
ClosedPublic

Authored by michaelrj on Feb 10 2023, 2:58 PM.

Details

Summary

The posix standard defines an alternate mode for printf where the
conversions also have an index that describes which argument to select.
Due to how variadic arguments work in C, to reach the nth argument all
n-1 previous arguments must be read with their correct types. If the
format string does not specify the types for a continuous set of
arguments, then the arguments after the discontinuity cannot be safely
read. This patch causes all conversions requesting an argument that
comes after a gap be treated as raw (i.e. the conversion string is
printed literally).

Diff Detail

Event Timeline

michaelrj created this revision.Feb 10 2023, 2:58 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptFeb 10 2023, 2:58 PM
michaelrj requested review of this revision.Feb 10 2023, 2:58 PM

fix another small bug found with the fuzzer

Mostly LGTM but I left a style comment inline.

libc/src/stdio/printf_core/parser.cpp
39–46

Readers would expect that a macro named GET_<> would be used a function macro which returns something. That aside, can this macro be replaced by a lambda?

michaelrj updated this revision to Diff 498502.Feb 17 2023, 1:59 PM

rename macro

libc/src/stdio/printf_core/parser.cpp
39–46

I don't think this macro can be replaced by a lambda because it needs to also take a type, and templating lambdas is a C++20 feature from what I can tell.

sivachandra accepted this revision.Feb 21 2023, 2:31 PM
This revision is now accepted and ready to land.Feb 21 2023, 2:31 PM
This revision was automatically updated to reflect the committed changes.