Bug 36312 - should refuse to overwrite input file with output file
Summary: should refuse to overwrite input file with output file
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: driver (show other bugs)
Version: 4.1.2
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, patch
: 40200 63462 66531 (view as bug list)
Depends on:
Blocks:
 
Reported: 2008-05-23 11:44 UTC by Shoaib Jameel
Modified: 2024-02-13 23:53 UTC (History)
9 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-10-06 00:00:00


Attachments
Possible fix (2.17 KB, patch)
2014-10-06 21:46 UTC, Manuel López-Ibáñez
Details | Diff
possible fix to overwriting source code (86.49 KB, patch)
2023-10-14 09:17 UTC, sarvel
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Shoaib Jameel 2008-05-23 11:44:12 UTC
Why doesn't gcc report an error or warning in such a case:
gcc filename.c -g -o filename.c

I have a very long C program, whose sources that got lost because of the above command. All I see in my program are some junk letters.

I know it's human error, but can you please incorporate any warning message about the above compile option. That the user is overwriting his/her own C code?
Comment 1 Richard Biener 2008-05-23 16:41:35 UTC
Happened to me as well.
Comment 2 Shoaib Jameel 2008-05-23 16:48:37 UTC
(In reply to comment #1)
> Happened to me as well.

any remedy...so as to recover the lost C codes?
Comment 3 Richard Biener 2008-05-23 17:01:03 UTC
I didn't lose anything - the internet backs up for me.
Comment 4 Shoaib Jameel 2008-05-23 17:05:54 UTC
(In reply to comment #3)
> I didn't lose anything - the internet backs up for me.

THe problem with me is rather grave. I dont even have a backup. Any method by which I can recover the source code? I need the codes desperately.
Comment 5 Samuel Tardieu 2008-10-07 10:53:40 UTC
A warning would be of no use, as it would be too late to recover the input file. Having GCC refuse to run in this case would be great.
Comment 6 Andrew Pinski 2014-10-06 06:29:40 UTC
*** Bug 63462 has been marked as a duplicate of this bug. ***
Comment 7 Manuel López-Ibáñez 2014-10-06 21:46:22 UTC
Created attachment 33656 [details]
Possible fix

The attached patch implements this for the driver and toplev.c. Probably the final version needs to move canonical_filename_eq to libiberty. It builds but I haven't tested it beyond a few quick tests. Perhaps it needs testcases.
Comment 8 Manuel López-Ibáñez 2014-11-05 17:24:18 UTC
Author: manu
Date: Wed Nov  5 17:23:46 2014
New Revision: 217149

URL: https://gcc.gnu.org/viewcvs?rev=217149&root=gcc&view=rev
Log:

gcc/testsuite/ChangeLog:

2014-11-05  Anthony Brandon  <anthony.brandon@gmail.com>

	PR driver/36312
	* gcc.misc-tests/output.exp: New test case for identical input and
	output files.

include/ChangeLog:

2014-11-05  Anthony Brandon  <anthony.brandon@gmail.com>

	PR driver/36312
	* filenames.h: Add prototype for canonical_filename_eq.

gcc/ChangeLog:

2014-11-05  Anthony Brandon  <anthony.brandon@gmail.com>

	PR driver/36312
	* diagnostic-core.h: Add prototype for fatal_error.
	* diagnostic.c (fatal_error): New function fatal_error.
	* gcc.c (store_arg): Remove have_o_argbuf_index.
	(process_command): Check if input and output files are the same.
	* toplev.c (init_asm_output): Check if input and output files are
	the same.

libiberty/ChangeLog:

2014-11-05  Anthony Brandon  <anthony.brandon@gmail.com>

	PR driver/36312
	* filename_cmp.c (canonical_filename_eq): New function to check if
	file names are the same.
	* functions.texi: Updated with documentation for new function.

Added:
    trunk/gcc/testsuite/gcc.misc-tests/output.exp
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/diagnostic-core.h
    trunk/gcc/diagnostic.c
    trunk/gcc/gcc.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/toplev.c
    trunk/include/ChangeLog
    trunk/include/filenames.h
    trunk/libiberty/ChangeLog
    trunk/libiberty/filename_cmp.c
    trunk/libiberty/functions.texi
Comment 9 Manuel López-Ibáñez 2014-11-05 17:32:33 UTC
Anthony Brandon updated my patch and added a testcase, so this is fixed in GCC 5.0. This was his second contribution to GCC, which shows that "Contributing to GCC is hard" is a myth.
Comment 10 Wojciech Koszek 2014-11-05 17:40:17 UTC
I'm very happy seeing it in GCC.

On Wed, Nov 5, 2014 at 9:24 AM, manu at gcc dot gnu.org <
gcc-bugzilla@gcc.gnu.org> wrote:

> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36312
>
> --- Comment #8 from Manuel López-Ibáñez <manu at gcc dot gnu.org> ---
> Author: manu
> Date: Wed Nov  5 17:23:46 2014
> New Revision: 217149
>
> URL: https://gcc.gnu.org/viewcvs?rev=217149&root=gcc&view=rev
> Log:
>
> gcc/testsuite/ChangeLog:
>
> 2014-11-05  Anthony Brandon  <anthony.brandon@gmail.com>
>
>     PR driver/36312
>     * gcc.misc-tests/output.exp: New test case for identical input and
>     output files.
>
> include/ChangeLog:
>
> 2014-11-05  Anthony Brandon  <anthony.brandon@gmail.com>
>
>     PR driver/36312
>     * filenames.h: Add prototype for canonical_filename_eq.
>
> gcc/ChangeLog:
>
> 2014-11-05  Anthony Brandon  <anthony.brandon@gmail.com>
>
>     PR driver/36312
>     * diagnostic-core.h: Add prototype for fatal_error.
>     * diagnostic.c (fatal_error): New function fatal_error.
>     * gcc.c (store_arg): Remove have_o_argbuf_index.
>     (process_command): Check if input and output files are the same.
>     * toplev.c (init_asm_output): Check if input and output files are
>     the same.
>
> libiberty/ChangeLog:
>
> 2014-11-05  Anthony Brandon  <anthony.brandon@gmail.com>
>
>     PR driver/36312
>     * filename_cmp.c (canonical_filename_eq): New function to check if
>     file names are the same.
>     * functions.texi: Updated with documentation for new function.
>
> Added:
>     trunk/gcc/testsuite/gcc.misc-tests/output.exp
> Modified:
>     trunk/gcc/ChangeLog
>     trunk/gcc/diagnostic-core.h
>     trunk/gcc/diagnostic.c
>     trunk/gcc/gcc.c
>     trunk/gcc/testsuite/ChangeLog
>     trunk/gcc/toplev.c
>     trunk/include/ChangeLog
>     trunk/include/filenames.h
>     trunk/libiberty/ChangeLog
>     trunk/libiberty/filename_cmp.c
>     trunk/libiberty/functions.texi
>
> --
> You are receiving this mail because:
> You are on the CC list for the bug.
>
Comment 11 Manuel López-Ibáñez 2014-11-05 22:17:54 UTC
Author: manu
Date: Wed Nov  5 22:17:22 2014
New Revision: 217159

URL: https://gcc.gnu.org/viewcvs?rev=217159&root=gcc&view=rev
Log:
Revert revision 217149 because it breaks Ada:

gcc/testsuite/ChangeLog:

2014-11-05  Anthony Brandon  <anthony.brandon@gmail.com>

        PR driver/36312
        * gcc.misc-tests/output.exp: New test case for identical input and
        output files.

include/ChangeLog:

2014-11-05  Anthony Brandon  <anthony.brandon@gmail.com>

        PR driver/36312
        * filenames.h: Add prototype for canonical_filename_eq.

gcc/ChangeLog:

2014-11-05  Anthony Brandon  <anthony.brandon@gmail.com>

        PR driver/36312
        * diagnostic-core.h: Add prototype for fatal_error.
        * diagnostic.c (fatal_error): New function fatal_error.
        * gcc.c (store_arg): Remove have_o_argbuf_index.
        (process_command): Check if input and output files are the same.
        * toplev.c (init_asm_output): Check if input and output files are
        the same.

libiberty/ChangeLog:

2014-11-05  Anthony Brandon  <anthony.brandon@gmail.com>

        PR driver/36312
        * filename_cmp.c (canonical_filename_eq): New function to check if
        file names are the same.
        * functions.texi: Updated with documentation for new function.

Removed:
    trunk/gcc/testsuite/gcc.misc-tests/output.exp
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/diagnostic-core.h
    trunk/gcc/diagnostic.c
    trunk/gcc/gcc.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/toplev.c
    trunk/include/ChangeLog
    trunk/include/filenames.h
    trunk/libiberty/ChangeLog
    trunk/libiberty/filename_cmp.c
    trunk/libiberty/functions.texi
Comment 12 Manuel López-Ibáñez 2014-11-05 23:58:07 UTC
Unfortunately, the patch broke Ada: https://gcc.gnu.org/ml/gcc-patches/2014-11/msg00368.html

so I have reverted it.
Comment 13 Manuel López-Ibáñez 2014-11-11 23:33:57 UTC
Author: manu
Date: Tue Nov 11 23:33:25 2014
New Revision: 217391

URL: https://gcc.gnu.org/viewcvs?rev=217391&root=gcc&view=rev
Log:
gcc/testsuite/ChangeLog:

2014-11-11  Anthony Brandon  <anthony.brandon@gmail.com>
	    Manuel López-Ibáñez  <manu@gcc.gnu.org>

	PR driver/36312
	* gcc.misc-tests/output.exp: New test case for identical input and
	output files.

include/ChangeLog:

2014-11-11  Anthony Brandon  <anthony.brandon@gmail.com>
	    Manuel López-Ibáñez  <manu@gcc.gnu.org>

	PR driver/36312
	* filenames.h: Add prototype for canonical_filename_eq.

gcc/ChangeLog:

2014-11-11  Anthony Brandon  <anthony.brandon@gmail.com>
	    Manuel López-Ibáñez  <manu@gcc.gnu.org>

	PR driver/36312
	* diagnostic-core.h: Add prototype for fatal_error.
	* diagnostic.c (fatal_error): New function fatal_error.
	* gcc.c (store_arg): Remove have_o_argbuf_index.
	(process_command): Check if input and output files are the same.
	* toplev.c (init_asm_output): Check if input and output files are
	the same.

libiberty/ChangeLog:

2014-11-11  Anthony Brandon  <anthony.brandon@gmail.com>
	    Manuel López-Ibáñez  <manu@gcc.gnu.org>

	PR driver/36312
	* filename_cmp.c (canonical_filename_eq): New function to check if
	file names are the same.
	* functions.texi: Updated with documentation for new function.

Added:
    trunk/gcc/testsuite/gcc.misc-tests/output.exp
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/diagnostic-core.h
    trunk/gcc/diagnostic.c
    trunk/gcc/gcc.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/toplev.c
    trunk/include/ChangeLog
    trunk/include/filenames.h
    trunk/libiberty/ChangeLog
    trunk/libiberty/filename_cmp.c
    trunk/libiberty/functions.texi
Comment 14 Manuel López-Ibáñez 2014-11-11 23:38:13 UTC
Fixed.
Comment 15 Carlos O'Donell 2014-11-19 14:40:01 UTC
This breaks glibc builds.

It is a common configure idiom to use /dev/null in both input and output files.

For example:

configure:72: checking for AVX support
configure:78: /home/triegel/local/gcc/bin/gcc -mavx -xc /dev/null -S -o /dev/null
gcc: fatal error: input file '/dev/null' is the same as output file
compilation terminated.
configure:81: $? = 1
configure:89: result: no

There is no need for a temporary file and it would slow down configure.

I expect that this change will break many more packages in the distribution builds because of this common idiom.

It would be sufficient if /dev/null were special cased in some way for Linux configurations.
Comment 16 Manuel López-Ibáñez 2014-11-19 15:23:33 UTC
(In reply to carlos from comment #15)
> It would be sufficient if /dev/null were special cased in some way for Linux
> configurations.

There is a pre-approved patch here (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63837#c12) that I'm testing. Unfortunately, there seems to be a problem with the machine in the compile farm that I use for gcc development, and I'm not able to finish a bootstrap&regression test. I always get:

couldn't create output pipe for command: too many open files

even for a pristine copy of GCC. This didn't happen a few days ago. I didn't have time yet to investigate what is wrong.
Comment 17 Jakub Jelinek 2014-11-19 18:12:26 UTC
Author: jakub
Date: Wed Nov 19 18:11:54 2014
New Revision: 217789

URL: https://gcc.gnu.org/viewcvs?rev=217789&root=gcc&view=rev
Log:
	PR driver/36312
	PR driver/63837
	* gcc.c (process_command): Don't check for input/output
	filename equality if output is HOST_BIT_BUCKET.
	* toplev.c (init_asm_output): Likewise.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/gcc.c
    trunk/gcc/toplev.c
Comment 18 Markus Trippelsdorf 2014-11-19 18:39:13 UTC
Fixed.
Comment 19 Marek Polacek 2015-06-22 18:12:57 UTC
*** Bug 66531 has been marked as a duplicate of this bug. ***
Comment 20 David Malcolm 2016-01-28 15:39:42 UTC
*** Bug 40200 has been marked as a duplicate of this bug. ***
Comment 21 Riccardo Mutschlechner 2016-10-14 03:15:00 UTC
Not sure if this is the best place to start, but here goes. I've noticed another similar issue.

Let's say you've compiled a binary, `test`, from a source `test.c`. If you then flip the arguments to gcc by mistake, `gcc -o test.c test` rather than `gcc -o test test.c`, you will overwrite the source file by trying to compile the binary, thus losing it permanently without some other backup.

Can I add some functionality that double checks against compiling *into* a source file extension? 

Is this cruft, or something that would actually be used - and if so, what is a good way to go about doing this? 

Would love to do it ASAP.
Comment 22 Manuel López-Ibáñez 2016-10-20 23:20:24 UTC
(In reply to Riccardo Mutschlechner from comment #21)
> Is this cruft, or something that would actually be used - and if so, what is
> a good way to go about doing this? 

https://gcc.gnu.org/wiki/GettingStarted#Basics:_Contributing_to_GCC_in_10_easy_steps
Comment 23 sarvel 2023-10-14 09:17:21 UTC
Created attachment 56105 [details]
possible fix to overwriting source code

added a possible fix, not sure whether thats the best approach as some source extensions may be missed

alternatively list of extensions for executable files may be used but *I think* it is easier to get list of all extensions for source files rather than executables
Comment 24 Andrew Pinski 2024-02-13 23:53:50 UTC
(In reply to Riccardo Mutschlechner from comment #21)
> Not sure if this is the best place to start, but here goes. I've noticed
> another similar issue.
> 
> Let's say you've compiled a binary, `test`, from a source `test.c`. If you
> then flip the arguments to gcc by mistake, `gcc -o test.c test` rather than
> `gcc -o test test.c`, you will overwrite the source file by trying to
> compile the binary, thus losing it permanently without some other backup.

That is PR 80182 really.