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

#mod_use crashes with an exception if asked to load an existing filename with no extension #7867

Closed
vicuna opened this issue Oct 26, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@vicuna
Copy link

commented Oct 26, 2018

Original bug ID: 7867
Reporter: ggole
Assigned to: @gasche
Status: resolved (set by @gasche on 2018-10-27T15:32:49Z)
Resolution: fixed
Priority: normal
Severity: crash
Version: 4.06.1
Fixed in version: 4.08.0+dev/beta1/beta2
Category: toplevel
Monitored by: @nojb @gasche

Bug description

When #mod_use is used with an argument that names a file that exists, and the name has no extension, the toplevel will die with an exception:

Fatal error: exception Invalid_argument("Filename.chop_extension")

Note that this does not happen if the named file does not exist.

Steps to reproduce

$ touch wat
$ ocaml
OCaml version 4.06.1

#mod_use "wat";;

Fatal error: exception Invalid_argument("Filename.chop_extension")
$

@vicuna

This comment has been minimized.

Copy link
Author

commented Oct 26, 2018

Comment author: @gasche

Nice catch. Would you like to provide a patch to fix this issue?

(It might be necessary to duplicate the fix for the native toplevel as well, see files toplevel/opttop*.)

@vicuna

This comment has been minimized.

Copy link
Author

commented Oct 26, 2018

Comment author: ggole

Hmm, is changing Filename.chop_extension to Filename.remove_extension sufficient? It seems as if that will avoid raising the exception.

The code (specifically parse_mod_use_file) seems a bit suspicious to me. A module name is supposed to be generated from a filename, but strings that are not sane module names such as "@%" are allowed. In fact, fairly sane filenames like "foo-bar.ml" will "work" and produce a module that the user will not be able to name in OCaml code - that doesn't seem right.

I would guess that a module name should be parsed after whacking off the filename parts?

@vicuna

This comment has been minimized.

Copy link
Author

commented Oct 26, 2018

Comment author: @gasche

For the record, the relevant code is:

let parse_mod_use_file name lb =
let modname =
String.capitalize_ascii (Filename.chop_extension (Filename.basename name))
in ...

Clearly the intent of the modname calculation is to turn a valid filename into a valid module name, and as you say it does not work very well. I don't think a robust logic exists to do this name munging inside the compiler (debugger/command_line.ml has similar but more robust logic in convert_module).

If you feel inspired to write something more robust, I think it could go into utils/misc.ml. If you don't, then I think at least getting rid of the failure here would be nice. Indeed Filename.remove_extension should be enough, I guess it did not exist at the time parse_mod_use_file was written.

@vicuna

This comment has been minimized.

Copy link
Author

commented Oct 27, 2018

Comment author: ggole

All right, I made a PR with that simple change.

@vicuna vicuna closed this Oct 27, 2018

@vicuna vicuna added the toplevel label Mar 14, 2019

@vicuna vicuna added the bug label Mar 20, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.