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

via libc-commits libc-commits at lists.llvm.org
Tue Jan 30 16:23:01 PST 2024


https://github.com/michaelrj-google created https://github.com/llvm/llvm-project/pull/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


>From e3ef9083c451d93f0be48e26430749687588eca6 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Tue, 30 Jan 2024 16:20:16 -0800
Subject: [PATCH] [libc] Fix condition ordering in scanf

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
---
 libc/src/stdio/scanf_core/float_converter.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

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