cmd: Make 'if errorlevel' only recognize integer values

Frédéric Delanoy frederic.delanoy at gmail.com
Wed Oct 26 09:57:52 CDT 2011


atoiW recognized invalid values, such as 1a
Only decimal notation is allowed, as Windows does
---
 programs/cmd/builtins.c                  |    9 ++++++++-
 programs/cmd/tests/test_builtins.cmd     |    6 ++++++
 programs/cmd/tests/test_builtins.cmd.exp |    2 ++
 3 files changed, 16 insertions(+), 1 deletions(-)

diff --git a/programs/cmd/builtins.c b/programs/cmd/builtins.c
index 721fb02..22e0342 100644
--- a/programs/cmd/builtins.c
+++ b/programs/cmd/builtins.c
@@ -1513,7 +1513,14 @@ void WCMD_if (WCHAR *p, CMD_LIST **cmdList) {
   WINE_TRACE("Condition: %s\n", wine_dbgstr_w(condition));
 
   if (!lstrcmpiW (condition, errlvlW)) {
-    test = (errorlevel >= atoiW(WCMD_parameter(p, 1+negate, NULL, NULL)));
+    WCHAR *param = WCMD_parameter(p, 1+negate, NULL, NULL);
+    WCHAR *endptr;
+    long int param_int = strtolW(param, &endptr, 10);
+    if (*endptr) {
+      WCMD_output_stderr(WCMD_LoadMessage(WCMD_SYNTAXERR));
+      return;
+    }
+    test = ((long int)errorlevel >= param_int);
     WCMD_parameter(p, 2+negate, &command, NULL);
   }
   else if (!lstrcmpiW (condition, existW)) {
diff --git a/programs/cmd/tests/test_builtins.cmd b/programs/cmd/tests/test_builtins.cmd
index 02e264d..a56d0a3 100644
--- a/programs/cmd/tests/test_builtins.cmd
+++ b/programs/cmd/tests/test_builtins.cmd
@@ -1485,10 +1485,16 @@ call :setError 1
 echo %ErrorLevel%
 if errorlevel 2 echo errorlevel too high, bad
 if errorlevel 1 echo errorlevel just right, good
+if errorlevel 01 echo errorlevel with leading zero just right, good
+if errorlevel -1 echo errorlevel with negative number OK
+if errorlevel 0x1 echo hexa should not be recognized!
+if errorlevel 1a echo invalid error level recognized!
 call :setError 0
 echo abc%ErrorLevel%def
 if errorlevel 1 echo errorlevel nonzero, bad
 if not errorlevel 1 echo errorlevel zero, good
+if not errorlevel 0x1 echo hexa should not be recognized!
+if not errorlevel 1a echo invalid error level recognized!
 rem Now verify that setting a real variable hides its magic variable
 set errorlevel=7
 echo %ErrorLevel% should be 7
diff --git a/programs/cmd/tests/test_builtins.cmd.exp b/programs/cmd/tests/test_builtins.cmd.exp
index 20e5d12..7a0eec3 100644
--- a/programs/cmd/tests/test_builtins.cmd.exp
+++ b/programs/cmd/tests/test_builtins.cmd.exp
@@ -792,6 +792,8 @@ localval
 9009
 1
 errorlevel just right, good
+errorlevel with leading zero just right, good
+errorlevel with negative number OK
 abc0def at or_broken@abc1def
 errorlevel zero, good at or_broken@errorlevel nonzero, bad
 7 should be 7
-- 
1.7.7




More information about the wine-patches mailing list