-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<regex>: add checks for integer overflow (#2169)
Co-authored-by: Alex Guteniev <gutenev@gmail.com> Co-authored-by: Nicole Mazzuca <mazzucan@outlook.com> Co-authored-by: Stephan T. Lavavej <stl@nuwen.net>
- Loading branch information
1 parent
fdb9c99
commit f241c79
Showing
6 changed files
with
91 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
RUNALL_INCLUDE ..\usual_matrix.lst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include <cassert> | ||
#include <regex> | ||
|
||
using namespace std; | ||
|
||
// GH-2168 <regex>: integer overflow on large backreference value | ||
int main() { | ||
try { | ||
// 4294967297 = 1 mod 2^32, so this will succeed if we don't check for overflow. | ||
regex testRegex{R"((a)\4294967297)", regex_constants::ECMAScript}; | ||
assert(false); | ||
} catch (const regex_error& e) { | ||
assert(e.code() == regex_constants::error_backref); | ||
} | ||
|
||
try { | ||
regex testRegex{"a{100000000000000000}", regex_constants::ECMAScript}; | ||
assert(false); | ||
} catch (const regex_error& e) { | ||
assert(e.code() == regex_constants::error_badbrace); | ||
} | ||
|
||
try { | ||
regex testRegex{"a{100,10000000000000000}", regex_constants::ECMAScript}; | ||
assert(false); | ||
} catch (const regex_error& e) { | ||
assert(e.code() == regex_constants::error_badbrace); | ||
} | ||
|
||
try { | ||
// 4294967296 = 0 mod 2^32, so this will succeed if we don't check for overflow. | ||
regex testRegex{R"([\4294967296-1])", regex_constants::ECMAScript}; | ||
assert(false); | ||
} catch (const regex_error& e) { | ||
assert(e.code() == regex_constants::error_escape); | ||
} | ||
|
||
// Also test 2147483648 = 2^31, the first value that overflows for int: | ||
|
||
try { | ||
regex testRegex{R"((a)\2147483648)", regex_constants::ECMAScript}; | ||
assert(false); | ||
} catch (const regex_error& e) { | ||
assert(e.code() == regex_constants::error_backref); | ||
} | ||
|
||
try { | ||
regex testRegex{"a{2147483648}", regex_constants::ECMAScript}; | ||
assert(false); | ||
} catch (const regex_error& e) { | ||
assert(e.code() == regex_constants::error_badbrace); | ||
} | ||
|
||
try { | ||
regex testRegex{"a{100,2147483648}", regex_constants::ECMAScript}; | ||
assert(false); | ||
} catch (const regex_error& e) { | ||
assert(e.code() == regex_constants::error_badbrace); | ||
} | ||
|
||
try { | ||
regex testRegex{R"([\2147483648-1])", regex_constants::ECMAScript}; | ||
assert(false); | ||
} catch (const regex_error& e) { | ||
assert(e.code() == regex_constants::error_escape); | ||
} | ||
} |