[libc-commits] [libc] [libc] Add a few missing casts (PR #70850)

Roland McGrath via libc-commits libc-commits at lists.llvm.org
Tue Oct 31 12:29:30 PDT 2023


https://github.com/frobtech created https://github.com/llvm/llvm-project/pull/70850

Stricter GCC warnings about implicit widening and narrowing cases necessitate additional explicit casts around some integer operations.

>From cf2fbf169d363086f36884c70c661e73c70244ce Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Tue, 31 Oct 2023 12:13:17 -0700
Subject: [PATCH] [libc] Add a few missing casts

Stricter GCC warnings about implicit widening and narrowing cases
necessitate additional explicit casts around some integer operations.
---
 libc/src/__support/integer_to_string.h | 6 +++---
 libc/src/stdio/printf_core/writer.h    | 4 ++--
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/libc/src/__support/integer_to_string.h b/libc/src/__support/integer_to_string.h
index 29f712461259444..6bbedac68efce2e 100644
--- a/libc/src/__support/integer_to_string.h
+++ b/libc/src/__support/integer_to_string.h
@@ -213,15 +213,15 @@ template <typename T, typename Fmt = radix::Dec> class IntegerToString {
 
     LIBC_INLINE static char digit_char(uint8_t digit) {
       if (digit < 10)
-        return '0' + digit;
-      return (Fmt::IS_UPPERCASE ? 'A' : 'a') + (digit - 10);
+        return '0' + static_cast<char>(digit);
+      return (Fmt::IS_UPPERCASE ? 'A' : 'a') + static_cast<char>(digit - 10);
     }
 
     LIBC_INLINE static void
     write_unsigned_number(UNSIGNED_T value,
                           details::BackwardStringBufferWriter &sink) {
       for (; sink.ok() && value != 0; value /= Fmt::BASE) {
-        const uint8_t digit(value % Fmt::BASE);
+        const uint8_t digit(static_cast<uint8_t>(value % Fmt::BASE));
         sink.push(digit_char(digit));
       }
     }
diff --git a/libc/src/stdio/printf_core/writer.h b/libc/src/stdio/printf_core/writer.h
index 1d4ff9916ee5f5c..e4f503abc34c52b 100644
--- a/libc/src/stdio/printf_core/writer.h
+++ b/libc/src/stdio/printf_core/writer.h
@@ -93,7 +93,7 @@ class Writer final {
   // Takes a string, copies it into the buffer if there is space, else passes it
   // to the overflow mechanism to be handled separately.
   LIBC_INLINE int write(cpp::string_view new_string) {
-    chars_written += new_string.size();
+    chars_written += static_cast<int>(new_string.size());
     if (LIBC_LIKELY(wb->buff_cur + new_string.size() <= wb->buff_len)) {
       inline_memcpy(wb->buff + wb->buff_cur, new_string.data(),
                     new_string.size());
@@ -107,7 +107,7 @@ class Writer final {
   // if there is space, else calls pad which will loop and call the overflow
   // mechanism on a secondary buffer.
   LIBC_INLINE int write(char new_char, size_t length) {
-    chars_written += length;
+    chars_written += static_cast<int>(length);
 
     if (LIBC_LIKELY(wb->buff_cur + length <= wb->buff_len)) {
       inline_memset(wb->buff + wb->buff_cur, new_char, length);



More information about the libc-commits mailing list