With -o elfcompress opens the output file with O_WRONLY and O_CREAT.
If the output file already existed then without O_TRUNC the file is
written from the start, but keeps all existing data. That means the
file might contain extra data if the (compressed) ELF file is shorter
than the existing file. Make sure to add O_TRUNC.

        * src/elfcompress.c (process_file): Add O_TRUNC to open call.

Signed-off-by: Mark Wielaard <[email protected]>
---
 src/elfcompress.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/elfcompress.c b/src/elfcompress.c
index 25ebd1be5c62..789bcb383786 100644
--- a/src/elfcompress.c
+++ b/src/elfcompress.c
@@ -616,7 +616,7 @@ process_file (const char *fname)
   else
     {
       fnew = xstrdup (foutput);
-      fdnew = open (fnew, O_WRONLY | O_CREAT, st.st_mode & ALLPERMS);
+      fdnew = open (fnew, O_WRONLY | O_CREAT | O_TRUNC, st.st_mode & ALLPERMS);
     }
 
   if (fdnew < 0)
-- 
2.53.0

Reply via email to