Skip to content

Commit c1f4182

Browse files
Denys Vlasenkormcc
Denys Vlasenko
authored andcommitted
zcat: complain if input is not compressed
function old new delta buffer_fill_and_print 178 191 +13 varvalue 735 743 +8 bbunpack 747 755 +8 open_zipped 85 89 +4 xmalloc_open_zipped_read_close 61 63 +2 get_addr_1 240 242 +2 fbsplash_main 1228 1230 +2 pstree_main 322 321 -1 builtin_type 121 119 -2 do_load 954 926 -28 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 7/3 up/down: 39/-31) Total: 8 bytes Signed-off-by: Denys Vlasenko <[email protected]>
1 parent a99f066 commit c1f4182

File tree

9 files changed

+17
-25
lines changed

9 files changed

+17
-25
lines changed

Diff for: archival/bbunzip.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,16 @@ int FAST_FUNC bbunpack(char **argv,
7272
goto err;
7373
} else {
7474
/* "clever zcat" with FILE */
75-
int fd = open_zipped(filename);
75+
/* fail_if_not_compressed because zcat refuses uncompressed input */
76+
int fd = open_zipped(filename, /*fail_if_not_compressed:*/ 1);
7677
if (fd < 0)
7778
goto err_name;
7879
xmove_fd(fd, STDIN_FILENO);
7980
}
8081
} else
8182
if (option_mask32 & SEAMLESS_MAGIC) {
8283
/* "clever zcat" on stdin */
83-
if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_detected*/ 0))
84+
if (setup_unzip_on_fd(STDIN_FILENO, /*fail_if_not_compressed*/ 1))
8485
goto err;
8586
}
8687

Diff for: archival/libarchive/get_header_tar.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ char FAST_FUNC get_header_tar(archive_handle_t *archive_handle)
241241
* or not first block (false positive, it's not .gz/.bz2!) */
242242
if (lseek(archive_handle->src_fd, -i, SEEK_CUR) != 0)
243243
goto err;
244-
if (setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_detected:*/ 0) != 0)
244+
if (setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_compressed:*/ 0) != 0)
245245
err:
246246
bb_error_msg_and_die("invalid tar magic");
247247
archive_handle->offset = 0;

Diff for: archival/libarchive/open_transformer.c

+5-14
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void FAST_FUNC open_transformer(int fd, const char *transform_prog)
118118
/* Used by e.g. rpm which gives us a fd without filename,
119119
* thus we can't guess the format from filename's extension.
120120
*/
121-
int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_detected)
121+
int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_compressed)
122122
{
123123
union {
124124
uint8_t b[4];
@@ -159,7 +159,7 @@ int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_detected)
159159
}
160160

161161
/* No known magic seen */
162-
if (fail_if_not_detected)
162+
if (fail_if_not_compressed)
163163
bb_error_msg_and_die("no gzip"
164164
IF_FEATURE_SEAMLESS_BZ2("/bzip2")
165165
IF_FEATURE_SEAMLESS_XZ("/xz")
@@ -180,7 +180,7 @@ int FAST_FUNC setup_unzip_on_fd(int fd, int fail_if_not_detected)
180180
return 0;
181181
}
182182

183-
int FAST_FUNC open_zipped(const char *fname)
183+
int FAST_FUNC open_zipped(const char *fname, int fail_if_not_compressed)
184184
{
185185
int fd;
186186

@@ -200,16 +200,7 @@ int FAST_FUNC open_zipped(const char *fname)
200200
|| (ENABLE_FEATURE_SEAMLESS_BZ2)
201201
|| (ENABLE_FEATURE_SEAMLESS_XZ)
202202
) {
203-
/*
204-
* Do we want to fail_if_not_detected?
205-
* In most cases, no: think "insmod non_compressed_module".
206-
* A case which would like to fail is "zcat uncompressed_file":
207-
* otherwise, it happily outputs uncompressed_file as-is,
208-
* which is, strictly speaking, not what is expected.
209-
* If this ever becomes a problem, we can add
210-
* fail_if_not_detected bool argument to open_zipped().
211-
*/
212-
setup_unzip_on_fd(fd, /*fail_if_not_detected:*/ 0);
203+
setup_unzip_on_fd(fd, fail_if_not_compressed);
213204
}
214205

215206
return fd;
@@ -222,7 +213,7 @@ void* FAST_FUNC xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_
222213
int fd;
223214
char *image;
224215

225-
fd = open_zipped(fname);
216+
fd = open_zipped(fname, /*fail_if_not_compressed:*/ 0);
226217
if (fd < 0)
227218
return NULL;
228219

Diff for: archival/rpm.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static void extract_cpio(int fd, const char *source_rpm)
122122
archive_handle->src_fd = fd;
123123
/*archive_handle->offset = 0; - init_handle() did it */
124124

125-
setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_detected:*/ 1);
125+
setup_unzip_on_fd(archive_handle->src_fd, /*fail_if_not_compressed:*/ 1);
126126
while (get_header_cpio(archive_handle) == EXIT_SUCCESS)
127127
continue;
128128
}

Diff for: archival/rpm2cpio.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ int rpm2cpio_main(int argc UNUSED_PARAM, char **argv)
8080
// signal(SIGCHLD, check_errors_in_children);
8181

8282
/* This works, but doesn't report uncompress errors (they happen in child) */
83-
setup_unzip_on_fd(rpm_fd, /*fail_if_not_detected:*/ 1);
83+
setup_unzip_on_fd(rpm_fd, /*fail_if_not_compressed:*/ 1);
8484
if (bb_copyfd_eof(rpm_fd, STDOUT_FILENO) < 0)
8585
bb_error_msg_and_die("error unpacking");
8686

Diff for: archival/tar.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ int tar_main(int argc UNUSED_PARAM, char **argv)
11361136
&& flags == O_RDONLY
11371137
&& !(opt & OPT_ANY_COMPRESS)
11381138
) {
1139-
tar_handle->src_fd = open_zipped(tar_filename);
1139+
tar_handle->src_fd = open_zipped(tar_filename, /*fail_if_not_compressed:*/ 0);
11401140
if (tar_handle->src_fd < 0)
11411141
bb_perror_msg_and_die("can't open '%s'", tar_filename);
11421142
} else {

Diff for: include/libbb.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -760,12 +760,12 @@ extern void *xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) FAS
760760

761761
#if SEAMLESS_COMPRESSION
762762
/* Autodetects gzip/bzip2 formats. fd may be in the middle of the file! */
763-
extern int setup_unzip_on_fd(int fd, int fail_if_not_detected) FAST_FUNC;
763+
extern int setup_unzip_on_fd(int fd, int fail_if_not_compressed) FAST_FUNC;
764764
/* Autodetects .gz etc */
765-
extern int open_zipped(const char *fname) FAST_FUNC;
765+
extern int open_zipped(const char *fname, int fail_if_not_compressed) FAST_FUNC;
766766
#else
767767
# define setup_unzip_on_fd(...) (0)
768-
# define open_zipped(fname) open((fname), O_RDONLY);
768+
# define open_zipped(fname, fail_if_not_compressed) open((fname), O_RDONLY);
769769
#endif
770770
extern void *xmalloc_open_zipped_read_close(const char *fname, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
771771

Diff for: miscutils/fbsplash.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ static void fb_drawimage(void)
353353
if (LONE_DASH(G.image_filename)) {
354354
theme_file = stdin;
355355
} else {
356-
int fd = open_zipped(G.image_filename);
356+
int fd = open_zipped(G.image_filename, /*fail_if_not_compressed:*/ 0);
357357
if (fd < 0)
358358
bb_simple_perror_msg_and_die(G.image_filename);
359359
theme_file = xfdopen_for_read(fd);

Diff for: miscutils/man.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static int run_pipe(const char *pager, char *man_filename, int man, int level)
102102

103103
ordinary_manpage:
104104
close(STDIN_FILENO);
105-
open_zipped(man_filename); /* guaranteed to use fd 0 (STDIN_FILENO) */
105+
open_zipped(man_filename, /*fail_if_not_compressed:*/ 0); /* guaranteed to use fd 0 (STDIN_FILENO) */
106106
/* "2>&1" is added so that nroff errors are shown in pager too.
107107
* Otherwise it may show just empty screen */
108108
cmd = xasprintf(

0 commit comments

Comments
 (0)