Skip to content

Commit c8e69fa

Browse files
committed
[libc] Fix GPU 'printf' on strings with padding
Summary: We get the `strlen` to know how much memory to allocate here, but it wasn't taking into account if the padding was larger than the string itself. This patch sets it to an empty string so we always add the minimum size. This implementation is slightly wasteful with memory, but I am not concerned with a few extra bytes here and there for some memory that gets immediately free'd.
1 parent 1d5d189 commit c8e69fa

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

libc/utils/gpu/server/rpc_server.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ void handle_printf(rpc::Server::Port &port) {
108108
if (cur_section.has_conv && cur_section.conv_name == 's' &&
109109
cur_section.conv_val_ptr) {
110110
strs_to_copy[lane].emplace_back(cur_section.conv_val_ptr);
111+
// Get the minimum size of the string in the case of padding.
112+
char c = '\0';
113+
cur_section.conv_val_ptr = &c;
114+
convert(&writer, cur_section);
111115
} else if (cur_section.has_conv) {
112116
// Ignore conversion errors for the first pass.
113117
convert(&writer, cur_section);

0 commit comments

Comments
 (0)