Opened 5 years ago

Closed 5 years ago

Last modified 4 years ago

#9930 closed bug (fixed)

By default, a source code without extension would get overwritten by executable on *nix

Reported by: Rufflewind Owned by: Rufflewind
Priority: normal Milestone: 7.10.1
Component: Compiler Version: 7.8.4
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Other Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

If I have a source code without an extension, say foo, and I forget to specify the output file via -o, GHC will overwrite it with the executable itself after compilation. This occurs only on *nix-like operating systems. Presumably something similar could happen on Windows – fortunately, those who use .exe for source code are probably among the minority.

Minimum example:

#!/bin/sh
echo >foo 'main=undefined'
ghc -x hs foo
less foo     # 'foo' now contains binary data

Change History (8)

comment:1 Changed 5 years ago by Rufflewind

Summary: By default, a source code would get overwritten by executable on *nixBy default, a source code without extension would get overwritten by executable on *nix

comment:2 Changed 5 years ago by Rufflewind

Owner: set to Rufflewind

comment:3 Changed 5 years ago by rwbarton

I would suggest that -o be required when the source filename does not have the expected file extension for its language (as specified by -x). I'd greatly prefer that to GHC trying to guess whether the user really meant to do what they asked GHC to do.

comment:4 in reply to:  3 Changed 5 years ago by Rufflewind

Replying to rwbarton:

I would suggest that -o be required when the source filename does not have the expected file extension for its language (as specified by -x).

I was thinking of failing with an error only if -o is not provided and the input and output filenames would coincide. It's a bit less restrictive than your suggestion and should cover the typical cases where this occurs. What do you think?

comment:5 Changed 5 years ago by Austin Seipp <austin@…>

In 78833ca6305f0875add94351592e141c032cd088/ghc:

Don't overwrite input file by default

Summary:
If the default filename of the output executable coincides with that of main
source file, throw an error instead of silently clobbering the input file.

Reviewers: austin

Reviewed By: austin

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D642

GHC Trac Issues: #9930

comment:6 Changed 5 years ago by thoughtpolice

Milestone: 7.10.1
Status: newmerge

Merged, thanks!

comment:7 Changed 5 years ago by thoughtpolice

Resolution: fixed
Status: mergeclosed

Merged to ghc-7.10 (via ed4de443b31bc383cc7194bef75d27c3a487e8de).

comment:8 Changed 4 years ago by Edward Z. Yang <ezyang@…>

In 5a65da43559908a42a701b4dbdff682dcdf2e121/ghc:

Don't run T9330fail on Windows, no clobber occurs. #9930

Signed-off-by: Edward Z. Yang <ezyang@cs.stanford.edu>
Note: See TracTickets for help on using tickets.