[PATCH] D101314: Fixed error that can be caused by invalid numbers [KALIEDOSCOPE TUTORIAL]
Sushma Unnibhavi via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 26 11:20:58 PDT 2021
sushmaunnibhavi created this revision.
sushmaunnibhavi added a reviewer: xgupta.
sushmaunnibhavi requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
There isn't proper error checking in the code : It will incorrectly read “1.23.45.67” and handle it as if you typed in “1.23”. Fixed this error.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D101314
Files:
llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl01.rst
llvm/examples/Kaleidoscope/Chapter2/toy.cpp
Index: llvm/examples/Kaleidoscope/Chapter2/toy.cpp
===================================================================
--- llvm/examples/Kaleidoscope/Chapter2/toy.cpp
+++ llvm/examples/Kaleidoscope/Chapter2/toy.cpp
@@ -48,14 +48,21 @@
return tok_identifier;
}
- if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
+ if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
std::string NumStr;
+ int count=0;/*Counts number of '.'. If it is greater than 1 should return false. For example if we have the input as “1.23.45.67”, it should return an error since this is invalid*/
do {
NumStr += LastChar;
LastChar = getchar();
+ if(LastChar == '.'){
+ count++;
+ }
+ if(count>1){
+ return LogError("Invalid number");;
+ }
} while (isdigit(LastChar) || LastChar == '.');
- NumVal = strtod(NumStr.c_str(), nullptr);
+ NumVal = strtod(NumStr.c_str(), 0);
return tok_number;
}
Index: llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl01.rst
===================================================================
--- llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl01.rst
+++ llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl01.rst
@@ -138,9 +138,16 @@
if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
std::string NumStr;
+ int count=0;/*Counts number of '.'. If it is greater than 1 should return error. For example if we have the input as “1.23.45.67”, it should return an error since this is invalid*/
do {
NumStr += LastChar;
LastChar = getchar();
+ if(LastChar == '.'){
+ count++;
+ }
+ if(count>1){
+ return LogError("Invalid number");
+ }
} while (isdigit(LastChar) || LastChar == '.');
NumVal = strtod(NumStr.c_str(), 0);
@@ -149,10 +156,7 @@
This is all pretty straightforward code for processing input. When
reading a numeric value from input, we use the C ``strtod`` function to
-convert it to a numeric value that we store in ``NumVal``. Note that
-this isn't doing sufficient error checking: it will incorrectly read
-"1.23.45.67" and handle it as if you typed in "1.23". Feel free to
-extend it! Next we handle comments:
+convert it to a numeric value that we store in ``NumVal``.Next we handle comments:
.. code-block:: c++
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101314.340592.patch
Type: text/x-patch
Size: 2416 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210426/d6aad090/attachment.bin>
More information about the llvm-commits
mailing list