Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
POTTIER Francois
menhir
Commits
5b05bee7
Commit
5b05bee7
authored
Jun 06, 2020
by
POTTIER Francois
Browse files
Harden the lexer against unreasonably large integer literals.
parent
8c0a6c5f
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/lexer.mll
View file @
5b05bee7
...
@@ -30,6 +30,18 @@ let error2 lexbuf =
...
@@ -30,6 +30,18 @@ let error2 lexbuf =
(* ------------------------------------------------------------------------ *)
(* ------------------------------------------------------------------------ *)
(* [int_of_string] raises [Failure] if its argument is too large. This is
not a problem in practice, but causes false positives when fuzzing
Menhir. We hide the problem by failing gracefully. *)
let
int_of_string
(
pos
:
Lexing
.
position
)
i
=
try
int_of_string
i
with
Failure
_
->
error1
pos
"unreasonably large integer."
(* ------------------------------------------------------------------------ *)
(* This wrapper saves the current lexeme start, invokes its argument,
(* This wrapper saves the current lexeme start, invokes its argument,
and restores it. This allows transmitting better positions to the
and restores it. This allows transmitting better positions to the
parser. *)
parser. *)
...
@@ -187,7 +199,7 @@ let position pos
...
@@ -187,7 +199,7 @@ let position pos
let
subject
,
check
=
let
subject
,
check
=
match
i
,
x
with
match
i
,
x
with
|
Some
i
,
None
->
|
Some
i
,
None
->
let
ii
=
int_of_string
i
in
(* cannot fail *)
let
ii
=
int_of_string
(
start_of_position
pos
)
i
in
if
ii
=
0
&&
where
=
WhereEnd
then
if
ii
=
0
&&
where
=
WhereEnd
then
(* [$endpos($0)] *)
(* [$endpos($0)] *)
Before
,
none
Before
,
none
...
@@ -569,7 +581,8 @@ and action percent openingpos monsters = parse
...
@@ -569,7 +581,8 @@ and action percent openingpos monsters = parse
{
let
_
,
monsters
=
parentheses
(
lexeme_start_p
lexbuf
)
monsters
lexbuf
in
{
let
_
,
monsters
=
parentheses
(
lexeme_start_p
lexbuf
)
monsters
lexbuf
in
action
percent
openingpos
monsters
lexbuf
}
action
percent
openingpos
monsters
lexbuf
}
|
'
$
'
([
'
0
'
-
'
9
'
]
+
as
i
)
|
'
$
'
([
'
0
'
-
'
9
'
]
+
as
i
)
{
let
monster
=
dollar
(
cpos
lexbuf
)
(
int_of_string
i
)
in
{
let
i
=
int_of_string
(
lexeme_start_p
lexbuf
)
i
in
let
monster
=
dollar
(
cpos
lexbuf
)
i
in
action
percent
openingpos
(
monster
::
monsters
)
lexbuf
}
action
percent
openingpos
(
monster
::
monsters
)
lexbuf
}
|
poskeyword
|
poskeyword
{
let
monster
=
position
(
cpos
lexbuf
)
where
flavor
i
x
in
{
let
monster
=
position
(
cpos
lexbuf
)
where
flavor
i
x
in
...
@@ -612,7 +625,8 @@ and parentheses openingpos monsters = parse
...
@@ -612,7 +625,8 @@ and parentheses openingpos monsters = parse
{ let _, monsters = action false (lexeme_start_p lexbuf) monsters lexbuf in
{ let _, monsters = action false (lexeme_start_p lexbuf) monsters lexbuf in
parentheses openingpos monsters lexbuf }
parentheses openingpos monsters lexbuf }
| '$' (['0'-'9']+ as i)
| '$' (['0'-'9']+ as i)
{ let monster = dollar (cpos lexbuf) (int_of_string i) in
{ let i = int_of_string (lexeme_start_p lexbuf) i in
let monster = dollar (cpos lexbuf) i in
parentheses openingpos (monster :: monsters) lexbuf }
parentheses openingpos (monster :: monsters) lexbuf }
| poskeyword
| poskeyword
{ let monster = position (cpos lexbuf) where flavor i x in
{ let monster = position (cpos lexbuf) where flavor i x in
...
...
test/static/bad/int-of-string.exp
0 → 100644
View file @
5b05bee7
File "int-of-string.mly", line 4, characters 10-10:
Error: unreasonably large integer.
test/static/bad/int-of-string.mly
0 → 100644
View file @
5b05bee7
(* this would lead to overflow, so int_of_string throws an exception. *)
%%
hiphop
:
{
$
12345678901234567891
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment