Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address short comings with printf for strings #715

Merged
merged 29 commits into from
Nov 6, 2024
Merged
Changes from 7 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9a2b9e2
keep this
Rekt3421 Aug 14, 2024
8046dc2
rework how process_printf handles strings
Rekt3421 Aug 14, 2024
4a33381
add missing space
Rekt3421 Aug 14, 2024
45f2ae4
fix cases with no format
Rekt3421 Aug 27, 2024
a7af5cb
use assert instead of if and return earlier.
Rekt3421 Aug 28, 2024
8c606c7
move data to end instead of asserting
Rekt3421 Aug 28, 2024
c7d90c4
Merge branch 'main' into 714
Rekt3421 Aug 28, 2024
1cd3365
reinstate comment format
Rekt3421 Aug 29, 2024
ff36633
reinstate comment format
Rekt3421 Aug 29, 2024
33e43e6
add unit test for original failure
Rekt3421 Sep 9, 2024
f1d81ce
Merge branch 'main' into 714
Rekt3421 Sep 9, 2024
ebec0c4
rework test logic
Rekt3421 Sep 9, 2024
467ffad
use correct class
Rekt3421 Sep 9, 2024
cbd9ff3
rework logic
Rekt3421 Sep 9, 2024
d0a44b6
get rid of sprintf
Rekt3421 Sep 11, 2024
7cca2d8
Fix issues with format_string
Rekt3421 Sep 24, 2024
f688a80
handle quotes
Rekt3421 Sep 25, 2024
9557840
fix vector of len>10 and %% cases
Rekt3421 Sep 26, 2024
2380ff8
make less complex
Rekt3421 Oct 1, 2024
852e14c
condense logic further
Rekt3421 Oct 2, 2024
49fc753
revert untouched code
Rekt3421 Oct 2, 2024
181a82c
commit romaric's suggestion
Rekt3421 Oct 2, 2024
41c9d79
remove uneeded checks
Rekt3421 Oct 3, 2024
39dda98
restore the condition
Rekt3421 Oct 3, 2024
9c25fbe
get rid of condition again
Rekt3421 Oct 7, 2024
6d85e0c
Add romaric's suggestion back in
Rekt3421 Oct 8, 2024
e7936e4
add two tests for new functionality
Rekt3421 Oct 16, 2024
c0a381c
add one last test
Rekt3421 Oct 16, 2024
b3a5159
fix test for while loop
Rekt3421 Oct 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 48 additions & 10 deletions src/printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,54 @@ void process_printf(char*& data, const printf_descriptor_map_t& descs,

// Firstly print the part of the format string up to the first '%'
size_t next_part = format_string.find_first_of('%');
if (next_part > format_string.size()) {
printf_out << format_string;
Rekt3421 marked this conversation as resolved.
Show resolved Hide resolved
printf("%s", printf_out.str().c_str());
data = data_end;
return;
}
printf_out << format_string.substr(0, next_part);

// Decompose the remaining format string into individual strings with
// one format specifier each, handle each one individually
size_t arg_idx = 0;
while (next_part < format_string.size() - 1) {
// Get the part of the format string before the next format specifier
// Get the part of the format string before the next format
// specifier
Rekt3421 marked this conversation as resolved.
Show resolved Hide resolved
size_t part_start = next_part;
size_t part_end = format_string.find_first_of('%', part_start + 1);

// Handle case where there's no second '%'
if (part_end == std::string::npos || part_end >= format_string.size()) {
part_end =
format_string.size(); // Set part_end to the end of the string
}
auto part_fmt = format_string.substr(part_start, part_end - part_start);

// Handle special cases
if (part_end == part_start + 1) {
printf_out << "%";
next_part = part_end + 1;
if (part_fmt == "%") {
// printf requires two % i.e. %% to display % iff they are not
// at the start of the string. All starting % shoulld be
// collapsed into one.
if (printf_out.str() != part_fmt) {
printf_out << part_fmt;
// check for two consecutive % since otherwise the way we
// are moving the next_part var this will be skipped.
if (format_string[part_start] == '%' &&
printf_out.str() != part_fmt) {
printf_out << part_fmt;
}
}
next_part = part_end;
// Skip the next % as well since we have already added it.
if (format_string[part_start] == '%') {
next_part++;
}
continue;
// Single char
} else if (part_fmt.length() == 1) {
printf_out << part_fmt;
next_part = part_end;
continue;
} else if (part_end == std::string::npos &&
arg_idx >= descs.at(printf_id).arg_sizes.size()) {
Expand Down Expand Up @@ -210,19 +243,24 @@ void process_printf(char*& data, const printf_descriptor_map_t& descs,
// Special case for %s
if (get_fmt_conversion(part_fmt) == 's') {
uint32_t string_id = read_buff<uint32_t>(data);
printf_out << print_part(
part_fmt, descs.at(string_id).format_string.c_str(), size);
if (string_id >= descs.size()) {
printf_out << "";
} else {
printf_out << print_part(
part_fmt, descs.at(string_id).format_string.c_str(),
size);
}
} else {
printf_out << print_part(part_fmt, data, size);
}
data += size;
} else {
// Vector argument
if (el_size == 0) {
// 'ele_size == 0' means that no length modifier has been used.
// According to the spec, this is an undefined behavior. Let's
// use the size coming from clspv and the vec_len to figure out
// the element size then.
// 'ele_size == 0' means that no length modifier has been
// used. According to the spec, this is an undefined
// behavior. Let's use the size coming from clspv and the
// vec_len to figure out the element size then.
Rekt3421 marked this conversation as resolved.
Show resolved Hide resolved
el_size = size / vec_len;
}
auto* data_start = data;
Expand Down