Index: gzip.c =================================================================== RCS file: /cvsroot/src/usr.bin/gzip/gzip.c,v retrieving revision 1.127 diff -p -u -r1.127 gzip.c --- gzip.c 1 Jun 2024 10:17:12 -0000 1.127 +++ gzip.c 4 Jan 2026 04:29:46 -0000 @@ -1,7 +1,7 @@ /* $NetBSD: gzip.c,v 1.127 2024/06/01 10:17:12 martin Exp $ */ /* - * Copyright (c) 1997-2024 Matthew R. Green + * Copyright (c) 1997-2026 Matthew R. Green * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -176,7 +176,7 @@ static suffixes_t suffixes[] = { #define NUM_SUFFIXES (sizeof suffixes / sizeof suffixes[0]) #define SUFFIX_MAXLEN 30 -static const char gzip_version[] = "NetBSD gzip 20240203"; +static const char gzip_version[] = "NetBSD gzip 20260103"; static int cflag; /* stdout mode */ static int dflag; /* decompress mode */ @@ -1805,7 +1805,9 @@ static void handle_stdin(void) { struct stat isb; - unsigned char fourbytes[4]; + unsigned char *prebuf = NULL; + size_t prebuf_len; + const size_t minbuf_len = 4; /* enough to identify the file */ size_t in_size; off_t usize, gsize; enum filetype method; @@ -1829,6 +1831,14 @@ handle_stdin(void) in_size = isb.st_size; else in_size = 0; + if (isb.st_blksize) + prebuf_len = isb.st_blksize; + else + prebuf_len = minbuf_len; + prebuf = malloc(prebuf_len); + if (prebuf == NULL) + maybe_err("malloc"); + infile_set("(stdin)", in_size); if (lflag) { @@ -1836,16 +1846,16 @@ handle_stdin(void) goto out; } - bytes_read = read_retry(STDIN_FILENO, fourbytes, sizeof fourbytes); + bytes_read = read_retry(STDIN_FILENO, prebuf, prebuf_len); if (bytes_read == -1) { maybe_warn("can't read stdin"); goto out; - } else if (bytes_read != sizeof(fourbytes)) { + } else if ((size_t)bytes_read < minbuf_len) { maybe_warnx("(stdin): unexpected end of file"); goto out; } - method = file_gettype(fourbytes); + method = file_gettype(prebuf); switch (method) { default: #ifndef SMALL @@ -1853,17 +1863,17 @@ handle_stdin(void) maybe_warnx("unknown compression format"); goto out; } - usize = cat_fd(fourbytes, sizeof fourbytes, &gsize, STDIN_FILENO); + usize = cat_fd(prebuf, bytes_read, &gsize, STDIN_FILENO); break; #endif case FT_GZIP: usize = gz_uncompress(STDIN_FILENO, STDOUT_FILENO, - (char *)fourbytes, sizeof fourbytes, &gsize, "(stdin)"); + (char *)prebuf, bytes_read, &gsize, "(stdin)"); break; #ifndef NO_BZIP2_SUPPORT case FT_BZIP2: usize = unbzip2(STDIN_FILENO, STDOUT_FILENO, - (char *)fourbytes, sizeof fourbytes, &gsize); + (char *)prebuf, bytes_read, &gsize); break; #endif #ifndef NO_COMPRESS_SUPPORT @@ -1873,27 +1883,27 @@ handle_stdin(void) goto out; } - usize = zuncompress(in, stdout, (char *)fourbytes, - sizeof fourbytes, &gsize); + usize = zuncompress(in, stdout, (char *)prebuf, + bytes_read, &gsize); fclose(in); break; #endif #ifndef NO_PACK_SUPPORT case FT_PACK: usize = unpack(STDIN_FILENO, STDOUT_FILENO, - (char *)fourbytes, sizeof fourbytes, &gsize); + (char *)prebuf, bytes_read, &gsize); break; #endif #ifndef NO_XZ_SUPPORT case FT_XZ: usize = unxz(STDIN_FILENO, STDOUT_FILENO, - (char *)fourbytes, sizeof fourbytes, &gsize); + (char *)prebuf, bytes_read, &gsize); break; #endif #ifndef NO_LZ_SUPPORT case FT_LZ: usize = unlz(STDIN_FILENO, STDOUT_FILENO, - (char *)fourbytes, sizeof fourbytes, &gsize); + (char *)prebuf, bytes_read, &gsize); break; #endif } @@ -1908,6 +1918,7 @@ handle_stdin(void) #endif out: + free(prebuf); infile_clear(); }