Skip to content

Commit

Permalink
printf: Fix string precision handling
Browse files Browse the repository at this point in the history
Previously we were assuming that the input string is always
null-terminated, which could lead to an out-of-bounds access if that
wasn't the case.
  • Loading branch information
qookei committed Mar 17, 2024
1 parent a5e28e1 commit 18d6231
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions include/frg/printf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,11 @@ void do_printf_chars(S &sink, char t, format_options opts,
if(!s)
s = "(null)";

int length = string_view{s}.size();
if(opts.precision && *opts.precision < length)
length = *opts.precision;
int length;
if(opts.precision)
length = generic_strnlen(s, *opts.precision);
else
length = generic_strlen(s);

if(opts.left_justify) {
for(int i = 0; i < length && s[i]; i++)
Expand All @@ -271,9 +273,11 @@ void do_printf_chars(S &sink, char t, format_options opts,
if(!s)
s = L"(null)";

int length = basic_string_view<wchar_t>{s}.size();
if(opts.precision && *opts.precision < length)
length = *opts.precision;
int length;
if(opts.precision)
length = generic_strnlen(s, *opts.precision);
else
length = generic_strlen(s);

if(opts.left_justify) {
for(int i = 0; i < length && s[i]; i++)
Expand Down

0 comments on commit 18d6231

Please sign in to comment.