[libc-commits] [PATCH] D124421: [libc] add printf writer

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Mon May 2 14:05:36 PDT 2022

sivachandra accepted this revision.
sivachandra added inline comments.

Comment at: libc/src/stdio/printf_core/string_writer.h:42
+  // cur_buffer, regardless of available_capacity.
+  void terminate() { *cur_buffer = '\0'; }
I suppose this method will be used by `s*printf` functions explicitly?

Comment at: libc/src/stdio/printf_core/writer.cpp:27
+  // regardless of max_length.
+  chars_written += length;
michaelrj wrote:
> sivachandra wrote:
> > The `+` operation here can also rollover. Is it OK?
> `chars_written` is supposed to store the value that will be returned when printf completes. The return value of printf is the number of characters written by that call as represented by an `int`, or a negative number if there was an error. For this purpose, all `chars_written` needs to do is be at least as large as an `int`, and if there would be overflow, return a negative number to represent the error. For this purpose I've changed `chars_written` to an `unsigned long long` so that it will definitely be bigger than an `int`, and the actual printf code can return a negative number if it would overflow that `int`. Finally, `unsigned long long` is guaranteed to have a maximum value of at least 18446744073709551615 (2^64 - 1), and if any call to printf writes more than that many bytes I'm declaring it undefined behavior.

  rG LLVM Github Monorepo



More information about the libc-commits mailing list