[vala/staging: 2/3] scanner: "\x" requires two hex digits at most and ignore leading zero #2



commit 88aa22af923e2cb452696d2f17822d4b88c70b79
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Mon Nov 25 09:35:53 2019 +0100

    scanner: "\x" requires two hex digits at most and ignore leading zero #2

 tests/Makefile.am                               |  2 ++
 tests/scanner/regex-escape-x-digit-length.test  |  2 +-
 tests/scanner/regex-escape-x-empty.test         |  5 +++
 tests/scanner/regex-escape-x.vala               |  2 +-
 tests/scanner/string-escape-x-digit-length.test |  2 +-
 tests/scanner/string-escape-x-empty.test        |  5 +++
 tests/scanner/string-escape-x.vala              |  2 +-
 vala/valageniescanner.vala                      | 33 +++++++++++++------
 vala/valascanner.vala                           | 42 +++++++++++++++++++------
 9 files changed, 73 insertions(+), 22 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a18e333c1..1bc7c4206 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -571,6 +571,7 @@ TESTS = \
        scanner/regex-escape-invalid.test \
        scanner/regex-escape-u-digit-length.test \
        scanner/regex-escape-x-digit-length.test \
+       scanner/regex-escape-x-empty.test \
        scanner/regex-escape-x.vala \
        scanner/regex-multiple-i.test \
        scanner/regex-multiple-m.test \
@@ -578,6 +579,7 @@ TESTS = \
        scanner/regex-multiple-x.test \
        scanner/string-escape-u-digit-length.test \
        scanner/string-escape-x-digit-length.test \
+       scanner/string-escape-x-empty.test \
        scanner/string-escape-x.vala \
        parser/assignment.vala \
        parser/attribute-duplicate.test \
diff --git a/tests/scanner/regex-escape-x-digit-length.test b/tests/scanner/regex-escape-x-digit-length.test
index 4c88c55ca..693415201 100644
--- a/tests/scanner/regex-escape-x-digit-length.test
+++ b/tests/scanner/regex-escape-x-digit-length.test
@@ -1,5 +1,5 @@
 Invalid Code
 
 void main () {
-       Regex foo = /\x/s;
+       Regex foo = /\xaaa/s;
 }
diff --git a/tests/scanner/regex-escape-x-empty.test b/tests/scanner/regex-escape-x-empty.test
new file mode 100644
index 000000000..4c88c55ca
--- /dev/null
+++ b/tests/scanner/regex-escape-x-empty.test
@@ -0,0 +1,5 @@
+Invalid Code
+
+void main () {
+       Regex foo = /\x/s;
+}
diff --git a/tests/scanner/regex-escape-x.vala b/tests/scanner/regex-escape-x.vala
index ebd59b16e..ee226c7dd 100644
--- a/tests/scanner/regex-escape-x.vala
+++ b/tests/scanner/regex-escape-x.vala
@@ -1,3 +1,3 @@
 void main () {
-       Regex foo = /\x10/s;
+       Regex foo = /\x10\x00\x00aa/s;
 }
diff --git a/tests/scanner/string-escape-x-digit-length.test b/tests/scanner/string-escape-x-digit-length.test
index f2134491b..89be3d98e 100644
--- a/tests/scanner/string-escape-x-digit-length.test
+++ b/tests/scanner/string-escape-x-digit-length.test
@@ -1,5 +1,5 @@
 Invalid Code
 
 void main () {
-       string foo = "\x";
+       string foo = "\xaaa";
 }
diff --git a/tests/scanner/string-escape-x-empty.test b/tests/scanner/string-escape-x-empty.test
new file mode 100644
index 000000000..f2134491b
--- /dev/null
+++ b/tests/scanner/string-escape-x-empty.test
@@ -0,0 +1,5 @@
+Invalid Code
+
+void main () {
+       string foo = "\x";
+}
diff --git a/tests/scanner/string-escape-x.vala b/tests/scanner/string-escape-x.vala
index 090c30998..316cbf824 100644
--- a/tests/scanner/string-escape-x.vala
+++ b/tests/scanner/string-escape-x.vala
@@ -1,3 +1,3 @@
 void main () {
-       string foo = "\x10";
+       string foo = "\x10\x00\x00aa";
 }
diff --git a/vala/valageniescanner.vala b/vala/valageniescanner.vala
index 26d34aff7..e6a1631fd 100644
--- a/vala/valageniescanner.vala
+++ b/vala/valageniescanner.vala
@@ -248,12 +248,17 @@ public class Vala.Genie.Scanner {
                                                        current++;
                                                        token_length_in_chars++;
                                                        int digit_length;
-                                                       for (digit_length = 0; digit_length < 2 && current < 
end && current[0].isxdigit (); digit_length++) {
+                                                       for (digit_length = 0; current < end && 
current[0].isxdigit ();) {
+                                                               if (current[0] != '0') {
+                                                                       digit_length++;
+                                                               }
                                                                current++;
                                                                token_length_in_chars++;
                                                        }
-                                                       if (digit_length != 2) {
-                                                               Report.error (get_source_reference 
(token_length_in_chars), "\\x requires two hex digits");
+                                                       if (digit_length < 1) {
+                                                               Report.error (get_source_reference 
(token_length_in_chars), "\\x used with no following hex digits");
+                                                       } else if (digit_length > 2) {
+                                                               Report.error (get_source_reference 
(token_length_in_chars), "hex escape sequence out of range");
                                                        }
                                                        break;
                                                default:
@@ -771,12 +776,17 @@ public class Vala.Genie.Scanner {
                                                        current++;
                                                        token_length_in_chars++;
                                                        int digit_length;
-                                                       for (digit_length = 0; digit_length < 2 && current < 
end && current[0].isxdigit (); digit_length++) {
+                                                       for (digit_length = 0; current < end && 
current[0].isxdigit ();) {
+                                                               if (current[0] != '0') {
+                                                                       digit_length++;
+                                                               }
                                                                current++;
                                                                token_length_in_chars++;
                                                        }
-                                                       if (digit_length < 1 || digit_length > 2) {
-                                                               Report.error (get_source_reference 
(token_length_in_chars), "\\x requires at least one or two hex digit");
+                                                       if (digit_length < 1) {
+                                                               Report.error (get_source_reference 
(token_length_in_chars), "\\x used with no following hex digits");
+                                                       } else if (digit_length > 2) {
+                                                               Report.error (get_source_reference 
(token_length_in_chars), "hex escape sequence out of range");
                                                        }
                                                        break;
                                                default:
@@ -1344,12 +1354,17 @@ public class Vala.Genie.Scanner {
                                                        current++;
                                                        token_length_in_chars++;
                                                        int digit_length;
-                                                       for (digit_length = 0; digit_length < 2 && current < 
end && current[0].isxdigit (); digit_length++) {
+                                                       for (digit_length = 0; current < end && 
current[0].isxdigit ();) {
+                                                               if (current[0] != '0') {
+                                                                       digit_length++;
+                                                               }
                                                                current++;
                                                                token_length_in_chars++;
                                                        }
-                                                       if (digit_length != 2) {
-                                                               Report.error (get_source_reference 
(token_length_in_chars), "\\x requires two hex digits");
+                                                       if (digit_length < 1) {
+                                                               Report.error (get_source_reference 
(token_length_in_chars), "\\x used with no following hex digits");
+                                                       } else if (digit_length > 2) {
+                                                               Report.error (get_source_reference 
(token_length_in_chars), "hex escape sequence out of range");
                                                        }
                                                        break;
                                                default:
diff --git a/vala/valascanner.vala b/vala/valascanner.vala
index e824fd618..9153c4bc4 100644
--- a/vala/valascanner.vala
+++ b/vala/valascanner.vala
@@ -233,12 +233,20 @@ public class Vala.Scanner {
                                                        current++;
                                                        token_length_in_chars++;
                                                        int digit_length;
-                                                       for (digit_length = 0; current < end && 
current[0].isxdigit (); digit_length++) {
+                                                       bool empty = true;
+                                                       for (digit_length = 0; current < end && 
current[0].isxdigit ();) {
+                                                               if (current[0] != '0') {
+                                                                       digit_length++;
+                                                               } else {
+                                                                       empty = false;
+                                                               }
                                                                current++;
                                                                token_length_in_chars++;
                                                        }
-                                                       if (digit_length < 1) {
-                                                               Report.error (get_source_reference 
(token_length_in_chars), "\\x requires at least one hex digit");
+                                                       if (empty && digit_length < 1) {
+                                                               Report.error (get_source_reference 
(token_length_in_chars), "\\x used with no following hex digits");
+                                                       } else if (digit_length > 2) {
+                                                               Report.error (get_source_reference 
(token_length_in_chars), "hex escape sequence out of range");
                                                        }
                                                        break;
                                                default:
@@ -745,12 +753,20 @@ public class Vala.Scanner {
                                                        current++;
                                                        token_length_in_chars++;
                                                        int digit_length;
-                                                       for (digit_length = 0; current < end && 
current[0].isxdigit (); digit_length++) {
+                                                       bool empty = true;
+                                                       for (digit_length = 0; current < end && 
current[0].isxdigit ();) {
+                                                               if (current[0] != '0') {
+                                                                       digit_length++;
+                                                               } else {
+                                                                       empty = false;
+                                                               }
                                                                current++;
                                                                token_length_in_chars++;
                                                        }
-                                                       if (digit_length < 1 || digit_length > 2) {
-                                                               Report.error (get_source_reference 
(token_length_in_chars), "\\x requires at least one or two hex digit");
+                                                       if (empty && digit_length < 1) {
+                                                               Report.error (get_source_reference 
(token_length_in_chars), "\\x used with no following hex digits");
+                                                       } else if (digit_length > 2) {
+                                                               Report.error (get_source_reference 
(token_length_in_chars), "hex escape sequence out of range");
                                                        }
                                                        break;
                                                default:
@@ -1180,12 +1196,20 @@ public class Vala.Scanner {
                                                        current++;
                                                        token_length_in_chars++;
                                                        int digit_length;
-                                                       for (digit_length = 0; current < end && 
current[0].isxdigit (); digit_length++) {
+                                                       bool empty = true;
+                                                       for (digit_length = 0; current < end && 
current[0].isxdigit ();) {
+                                                               if (current[0] != '0') {
+                                                                       digit_length++;
+                                                               } else {
+                                                                       empty = false;
+                                                               }
                                                                current++;
                                                                token_length_in_chars++;
                                                        }
-                                                       if (digit_length < 1) {
-                                                               Report.error (get_source_reference 
(token_length_in_chars), "\\x requires at least one hex digit");
+                                                       if (empty && digit_length < 1) {
+                                                               Report.error (get_source_reference 
(token_length_in_chars), "\\x used with no following hex digits");
+                                                       } else if (digit_length > 2) {
+                                                               Report.error (get_source_reference 
(token_length_in_chars), "hex escape sequence out of range");
                                                        }
                                                        break;
                                                default:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]