Merge lp:~yavor-nikolov/pbzip2/bug-807536-excessive-output-permissions into lp:pbzip2/1.1

Proposed by Yavor Nikolov on 2011-07-11
Status: Merged
Merged at revision: 23
Proposed branch: lp:~yavor-nikolov/pbzip2/bug-807536-excessive-output-permissions
Merge into: lp:pbzip2/1.1
Diff against target: 158 lines (+65/-13)
3 files modified
ChangeLog (+2/-0)
pbzip2.cpp (+61/-11)
pbzip2.h (+2/-2)
To merge this branch: bzr merge lp:~yavor-nikolov/pbzip2/bug-807536-excessive-output-permissions
Reviewer Review Type Date Requested Status
pbzip2 development team 2011-07-11 Pending
Review via email: mp+67630@code.launchpad.net

Commit message

Merged fix for bug #807536 (excessive output permissions while compress/decompress is in progress)

Description of the change

Fixed excessive output permissions while compress/decompress is in progress (bug #807536)

To post a comment you must log in.
24. By Yavor Nikolov on 2011-07-13

Fixed excessive output permissions during compress/decompress in case of output file already existed (additional fix for bug #807536).

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ChangeLog'
2--- ChangeLog 2011-07-11 18:20:53 +0000
3+++ ChangeLog 2011-07-13 22:04:28 +0000
4@@ -1,4 +1,6 @@
5 Changes in 1.1.5 (NOT RELEASED YET)
6+- Fixed excessive output permissions while compress/decompress
7+ is in progress (bug #807536)
8 Changes in 1.1.4 (Apr 22, 2011)
9 - Fixed hang on decompress with --ignore-trailing-garbage=1 when
10 producer is interrupted on trailing garbage (bug #762464)
11
12=== modified file 'pbzip2.cpp'
13--- pbzip2.cpp 2011-07-11 18:20:53 +0000
14+++ pbzip2.cpp 2011-07-13 22:04:28 +0000
15@@ -194,6 +194,9 @@
16 * - Print trailing garbage errors even when in quiet mode (bug #743635)
17 * - Fixed hang on decompress with --ignore-trailing-garbage=1 when
18 * producer is interrupted on trailing garbage (bug #762464)
19+ * - Fixed excessive output permissions while compress/decompress
20+ * is in progress (bug #807536)
21+ *
22 *
23 *
24 * Specials thanks for suggestions and testing: Phillippe Welsh,
25@@ -566,6 +569,53 @@
26 return (count - bytesRemaining);
27 }
28
29+/*
30+ * Open output file with least required privileges
31+ */
32+int safe_open_output(const char *path)
33+{
34+ return open(path, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, FILE_MODE);
35+}
36+
37+/*
38+ * Based on bzip2.c code
39+ */
40+FILE *safe_fopen_output(const char *path, const char *mode)
41+{
42+ int fh = safe_open_output(path);
43+ if (fh == -1)
44+ {
45+ return NULL;
46+ }
47+
48+ FILE *fp = fdopen(fh, mode);
49+ if (fp == NULL)
50+ {
51+ close(fh);
52+ }
53+
54+ return fp;
55+}
56+
57+/**
58+ * Check if a given file exists.
59+ *
60+ * @return true if file exists and false if it doesn't
61+ */
62+bool check_file_exists( const char * filename )
63+{
64+ int hOutfile = open( filename, O_RDONLY | O_BINARY );
65+
66+ if ( hOutfile == -1 )
67+ {
68+ return false;
69+ }
70+ else
71+ {
72+ close( hOutfile );
73+ return true;
74+ }
75+}
76
77 /*
78 *********************************************************
79@@ -1658,7 +1708,7 @@
80 // write to file instead of stdout
81 if (OutputStdOut == 0)
82 {
83- hOutfile = open(OutFilename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, FILE_MODE);
84+ hOutfile = safe_open_output(OutFilename);
85 // check to see if file creation was successful
86 if (hOutfile == -1)
87 {
88@@ -1913,7 +1963,7 @@
89 // write to file instead of stdout
90 if (OutputStdOut == 0)
91 {
92- hOutfile = open(OutFilename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, FILE_MODE);
93+ hOutfile = safe_open_output(OutFilename);
94 // check to see if file creation was successful
95 if (hOutfile == -1)
96 {
97@@ -2156,7 +2206,7 @@
98 // see if we are outputting to stdout
99 if (OutputStdOut == 0)
100 {
101- stream = fopen(OutFilename, "wb");
102+ stream = safe_fopen_output(OutFilename, "wb");
103 if (stream == NULL)
104 {
105 handle_error(EF_NOQUIT, -1,
106@@ -4040,19 +4090,19 @@
107 }
108
109 // check to see if output file exists
110- if ((force != 1) && (OutputStdOut == 0))
111- {
112- hOutfile = open(outFilename.c_str(), O_RDONLY | O_BINARY);
113- // check to see if file exists before processing
114- if (hOutfile != -1)
115+ if ((OutputStdOut == 0) && check_file_exists(outFilename.c_str()))
116+ {
117+ if (force != 1)
118 {
119 fprintf(stderr, "pbzip2: *ERROR: Output file [%s] already exists! Use -f to overwrite...\n", outFilename.c_str());
120 fprintf(stderr, "-------------------------------------------\n");
121 errLevel = 1;
122- close(hOutfile);
123- errLevel = 1;
124 continue;
125 }
126+ else
127+ {
128+ remove(outFilename.c_str());
129+ }
130 }
131
132 if (readEntireFile == 1)
133@@ -4125,7 +4175,7 @@
134 // write to file instead of stdout
135 if (OutputStdOut == 0)
136 {
137- hOutfile = open(outFilename.c_str(), O_RDWR | O_CREAT | O_TRUNC | O_BINARY, FILE_MODE);
138+ hOutfile = safe_open_output(outFilename.c_str());
139 // check to see if file creation was successful
140 if (hOutfile == -1)
141 {
142
143=== modified file 'pbzip2.h'
144--- pbzip2.h 2011-02-13 15:00:47 +0000
145+++ pbzip2.h 2011-07-13 22:04:28 +0000
146@@ -16,11 +16,11 @@
147 #include <string>
148 #include <cctype>
149
150+#define FILE_MODE (S_IRUSR | S_IWUSR )
151+
152 #ifndef WIN32
153-#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
154 #define OFF_T off_t
155 #else
156-#define FILE_MODE (S_IRUSR | S_IWUSR )
157 #define OFF_T __int64
158 #endif
159

Subscribers

People subscribed via source and target branches