[libc-commits] [PATCH] D139891: [libc] fix scanf error handling

Michael Jones via Phabricator via libc-commits libc-commits at lists.llvm.org
Mon Dec 12 16:02:40 PST 2022


michaelrj created this revision.
michaelrj added reviewers: sivachandra, lntue.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added projects: libc-project, All.
michaelrj requested review of this revision.

Scanf is supposed to return EOF when it fails to make any conversions
and there is an input failure. Previously it would return EOF on a
matching failure, which may be an input failure but can also be a
parsing error.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139891

Files:
  libc/src/stdio/scanf_core/file_reader.h
  libc/src/stdio/scanf_core/reader.cpp
  libc/src/stdio/scanf_core/reader.h
  libc/src/stdio/scanf_core/scanf_main.cpp


Index: libc/src/stdio/scanf_core/scanf_main.cpp
===================================================================
--- libc/src/stdio/scanf_core/scanf_main.cpp
+++ libc/src/stdio/scanf_core/scanf_main.cpp
@@ -35,7 +35,7 @@
     }
   }
 
-  if (conversions == 0 && ret_val != READ_OK) {
+  if (conversions == 0 && reader->has_error()) {
     // This is intended to be converted to EOF in the client call to avoid
     // including stdio.h in this internal file.
     return -1;
Index: libc/src/stdio/scanf_core/reader.h
===================================================================
--- libc/src/stdio/scanf_core/reader.h
+++ libc/src/stdio/scanf_core/reader.h
@@ -45,6 +45,8 @@
   void ungetc(char c);
 
   size_t chars_read() { return cur_chars_read; }
+
+  bool has_error();
 };
 
 } // namespace scanf_core
Index: libc/src/stdio/scanf_core/reader.cpp
===================================================================
--- libc/src/stdio/scanf_core/reader.cpp
+++ libc/src/stdio/scanf_core/reader.cpp
@@ -33,5 +33,12 @@
   }
 }
 
+bool Reader::has_error() {
+  if (reader_type == ReaderType::File) {
+    return file_reader->has_error();
+  }
+  return false;
+}
+
 } // namespace scanf_core
 } // namespace __llvm_libc
Index: libc/src/stdio/scanf_core/file_reader.h
===================================================================
--- libc/src/stdio/scanf_core/file_reader.h
+++ libc/src/stdio/scanf_core/file_reader.h
@@ -30,6 +30,7 @@
 
   char get_char();
   void unget_char(char c);
+  bool has_error() { return file->error_unlocked(); }
 };
 
 } // namespace scanf_core


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139891.482301.patch
Type: text/x-patch
Size: 1588 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20221213/52e637a2/attachment-0001.bin>


More information about the libc-commits mailing list