Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camlp4 loops endlessly due to faulty "ignore_upto" function : #4966

Closed
vicuna opened this issue Jan 26, 2010 · 1 comment
Closed

Camlp4 loops endlessly due to faulty "ignore_upto" function : #4966

vicuna opened this issue Jan 26, 2010 · 1 comment

Comments

@vicuna
Copy link

vicuna commented Jan 26, 2010

Original bug ID: 4966
Reporter: bluestorm
Assigned to: ertai
Status: closed (set by @xavierleroy on 2012-09-25T18:07:21Z)
Resolution: fixed
Priority: normal
Severity: minor
Category: -for Camlp4 use https://github.com/ocaml/camlp4/issues
Monitored by: @gasche

Bug description

The ignore_upto of the stream testers (ugly camlp4 hacks by themselves) are faulty : they do not handle the "EOI" token gracefully, leading in some case to diverging loops.

Camlp4ListComprehension and Camlp4OCamlParser are affected, 3.10, 3.11 and Ocaml SVN trunk. See "additional information" for tests and a fix.

Additional information

Here is a two-lines (incorrect) caml code leading to an infinite loop when parsed by camlp4o :

class type foo = object end
class test : [int -> foo = fun n -> object end

The loops is caused by the "[" : at this syntaxic position is a "test_ctyp_minusgreater" stream test, using the faulty "ignore-upto" function.

Here is the faulty ignore_upto code :

    and ignore_upto end_kwd n =
      match stream_peek_nth n strm with
      [ Some (KEYWORD prm) when prm = end_kwd -> n
      | Some (KEYWORD ("[" | "[<")) ->
          ignore_upto end_kwd (ignore_upto "]" (n + 1) + 1)
      | Some (KEYWORD "(") -> ignore_upto end_kwd (ignore_upto ")" (n + 1) + 1)
      | Some _ -> ignore_upto end_kwd (n + 1)
      | None -> raise Stream.Failure ]
    in

Here is a (hopefully) correct fix :

    and ignore_upto end_kwd n =
      match stream_peek_nth n strm with
      [ Some (KEYWORD prm) when prm = end_kwd -> n 
      | Some (KEYWORD ("[" | "[<")) ->
          ignore_upto end_kwd (ignore_upto "]" (n + 1) + 1)
      | Some (KEYWORD "(") ->
          ignore_upto end_kwd (ignore_upto ")" (n + 1) + 1)
      | Some (KEYWORD "{") -> 
          ignore_upto end_kwd (ignore_upto "}" (n + 1) + 1)        
      | None | Some EOI -> raise Stream.Failure
      | Some _ -> ignore_upto end_kwd (n + 1) ]

The only change being the new (Some EOI) pattern.

This bug is also present in Camlp4ListComprehension, in wich the same ignore_upto code is reproduced verbatim. Here is a test:

camlp4o -parser Camlp4ListComprehension -str '[ x | (x <- l ]'

Same cause, same fix.

The faulty code is still present in Ocaml SVN trunk, so I suppose it wasn't fixed.

The bug was originally reported by Tiphaine Turpin on the "Batteries" project bug tracker : the buggy file, pa_comprehension.ml, use code borrowed from Camlp4ListComprehension.

@vicuna
Copy link
Author

vicuna commented Jun 16, 2010

Comment author: ertai

Fixed. Thanks for the report.

Thanks to the new TRY feature. We gotten rid of this hack, and as we say: "Code removed is code debugged!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant