[libc-commits] [libc] 22773e5 - [libc] Fix condition ordering in scanf (#80083)

via libc-commits libc-commits at lists.llvm.org
Wed Jan 31 13:25:54 PST 2024


Author: michaelrj-google
Date: 2024-01-31T13:25:49-08:00
New Revision: 22773e591fc3e585a32f8122eb203e50fcc1e441

URL: https://github.com/llvm/llvm-project/commit/22773e591fc3e585a32f8122eb203e50fcc1e441
DIFF: https://github.com/llvm/llvm-project/commit/22773e591fc3e585a32f8122eb203e50fcc1e441.diff

LOG: [libc] Fix condition ordering in scanf (#80083)

The inf and nan string index bounds checks were after the index was
being used. This patch moves the index usage to the end of the
condition.

Fixes #79988

Added: 
    

Modified: 
    libc/src/stdio/scanf_core/float_converter.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/stdio/scanf_core/float_converter.cpp b/libc/src/stdio/scanf_core/float_converter.cpp
index 47a43d58ba7c7..8500d98f4121a 100644
--- a/libc/src/stdio/scanf_core/float_converter.cpp
+++ b/libc/src/stdio/scanf_core/float_converter.cpp
@@ -57,8 +57,8 @@ int convert_float(Reader *reader, const FormatSection &to_conv) {
   if (to_lower(cur_char) == inf_string[0]) {
     size_t inf_index = 0;
 
-    for (; to_lower(cur_char) == inf_string[inf_index] &&
-           inf_index < sizeof(inf_string) && out_str.length() < max_width;
+    for (; inf_index < sizeof(inf_string) && out_str.length() < max_width &&
+           to_lower(cur_char) == inf_string[inf_index];
          ++inf_index) {
       if (!out_str.append(cur_char)) {
         return ALLOCATION_FAILURE;
@@ -80,8 +80,8 @@ int convert_float(Reader *reader, const FormatSection &to_conv) {
   if (to_lower(cur_char) == nan_string[0]) {
     size_t nan_index = 0;
 
-    for (; to_lower(cur_char) == nan_string[nan_index] &&
-           nan_index < sizeof(nan_string) && out_str.length() < max_width;
+    for (; nan_index < sizeof(nan_string) && out_str.length() < max_width &&
+           to_lower(cur_char) == nan_string[nan_index];
          ++nan_index) {
       if (!out_str.append(cur_char)) {
         return ALLOCATION_FAILURE;


        


More information about the libc-commits mailing list