Skip to content

[libc] Fix condition ordering in scanf #80083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2024

Conversation

michaelrj-google
Copy link
Contributor

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

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 llvm#79988
@llvmbot llvmbot added the libc label Jan 31, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 31, 2024

@llvm/pr-subscribers-libc

Author: None (michaelrj-google)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/80083.diff

1 Files Affected:

  • (modified) libc/src/stdio/scanf_core/float_converter.cpp (+4-4)
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;

@michaelrj-google michaelrj-google merged commit 22773e5 into llvm:main Jan 31, 2024
@michaelrj-google michaelrj-google deleted the libcScanfFix branch January 31, 2024 21:25
carlosgalvezp pushed a commit to carlosgalvezp/llvm-project that referenced this pull request Feb 1, 2024
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 llvm#79988
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

libc/src/stdio/scanf_core/float_converter.cpp: 2 * sanity check in wrong place ?
3 participants