From 49a8ad2487b2329a538450200f1b1b2d698d6a23 Mon Sep 17 00:00:00 2001 From: kientzle Date: Sat, 14 Aug 2004 03:45:45 +0000 Subject: [PATCH] Eliminate reliance on non-portable by implementing a very simple errx() function. Improve behavior when bzlib/zlib are missing by detecting and issuing an error message on attempts to read gzip/bzip2 compressed archives. --- lib/libarchive/Makefile | 2 +- lib/libarchive/archive_private.h | 2 + lib/libarchive/archive_read.c | 15 ++++--- .../archive_read_support_compression_bzip2.c | 41 +++++++++++++---- ...rchive_read_support_compression_compress.c | 6 +-- .../archive_read_support_compression_gzip.c | 44 ++++++++++++++----- .../archive_read_support_format_cpio.c | 1 - .../archive_read_support_format_tar.c | 13 ++++-- lib/libarchive/archive_string.c | 6 ++- lib/libarchive/archive_string_sprintf.c | 1 - lib/libarchive/archive_util.c | 12 ++++- 11 files changed, 104 insertions(+), 39 deletions(-) diff --git a/lib/libarchive/Makefile b/lib/libarchive/Makefile index 4ffff1bb14a9..b4be6469f85d 100644 --- a/lib/libarchive/Makefile +++ b/lib/libarchive/Makefile @@ -7,7 +7,7 @@ LIB= archive -VERSION= 1.01.010 +VERSION= 1.01.013 ARCHIVE_API_FEATURE= 2 ARCHIVE_API_VERSION= 1 SHLIB_MAJOR= ${ARCHIVE_API_VERSION} diff --git a/lib/libarchive/archive_private.h b/lib/libarchive/archive_private.h index 5fb4d9bd982f..d9d4713e0f3d 100644 --- a/lib/libarchive/archive_private.h +++ b/lib/libarchive/archive_private.h @@ -235,6 +235,8 @@ int __archive_read_register_compression(struct archive *a, int (*bid)(const void *, size_t), int (*init)(struct archive *, const void *, size_t)); +void __archive_errx(int retvalue, const char *msg); + #define err_combine(a,b) ((a) < (b) ? (a) : (b)) #endif diff --git a/lib/libarchive/archive_read.c b/lib/libarchive/archive_read.c index 2f9a998e4214..39ff7a8ce128 100644 --- a/lib/libarchive/archive_read.c +++ b/lib/libarchive/archive_read.c @@ -35,7 +35,6 @@ #include "archive_platform.h" __FBSDID("$FreeBSD$"); -#include #include #include #include @@ -110,8 +109,8 @@ archive_read_open(struct archive *a, void *client_data, archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW); if (reader == NULL) - errx(1, - "Fatal: No reader function provided to archive_read_open"); + __archive_errx(1, + "No reader function provided to archive_read_open"); a->client_reader = reader; a->client_opener = opener; @@ -181,7 +180,7 @@ choose_decompressor(struct archive *a, const void *buffer, size_t bytes_read) * and demands a quick and definitive abort. */ if (best_bid_slot < 0) - errx(1, "Fatal: No decompressors were registered; you " + __archive_errx(1, "No decompressors were registered; you " "must call at least one " "archive_read_support_compression_XXX function in order " "to successfully read an archive."); @@ -304,7 +303,7 @@ choose_format(struct archive *a) * and demands a quick and definitive abort. */ if (best_bid_slot < 0) - errx(1, "Fatal: No formats were registered; you must " + __archive_errx(1, "No formats were registered; you must " "invoke at least one archive_read_support_format_XXX " "function in order to successfully read an archive."); @@ -526,7 +525,8 @@ __archive_read_register_format(struct archive *a, } } - errx(1, "Fatal: Not enough slots for format registration"); + __archive_errx(1, "Not enough slots for format registration"); + return (ARCHIVE_FATAL); /* Never actually called. */ } /* @@ -554,5 +554,6 @@ __archive_read_register_compression(struct archive *a, } } - errx(1, "Fatal: Not enough slots for compression registration"); + __archive_errx(1, "Not enough slots for compression registration"); + return (ARCHIVE_FATAL); /* Never actually executed. */ } diff --git a/lib/libarchive/archive_read_support_compression_bzip2.c b/lib/libarchive/archive_read_support_compression_bzip2.c index e65d0acf99ba..8b82696c5f63 100644 --- a/lib/libarchive/archive_read_support_compression_bzip2.c +++ b/lib/libarchive/archive_read_support_compression_bzip2.c @@ -26,21 +26,20 @@ #include "archive_platform.h" -/* Don't compile this if we don't have bzlib. */ -#if HAVE_BZLIB_H - __FBSDID("$FreeBSD$"); -#include #include #include #include #include +#ifdef HAVE_BZLIB_H #include +#endif #include "archive.h" #include "archive_private.h" +#if HAVE_BZLIB_H struct private_data { bz_stream stream; unsigned char *uncompressed_buffer; @@ -49,12 +48,15 @@ struct private_data { int64_t total_out; }; -static int bid(const void *, size_t); static int finish(struct archive *); -static int init(struct archive *, const void *, size_t); static ssize_t read_ahead(struct archive *, const void **, size_t); static ssize_t read_consume(struct archive *, size_t); static int drive_decompressor(struct archive *a, struct private_data *); +#endif + +/* These two functions are defined even if we lack bzlib. See below. */ +static int bid(const void *, size_t); +static int init(struct archive *, const void *, size_t); int archive_read_support_compression_bzip2(struct archive *a) @@ -116,6 +118,28 @@ bid(const void *buff, size_t len) return (bits_checked); } +#ifndef HAVE_BZLIB_H + +/* + * If we don't have bzlib on this system, we can't actually do the + * decompression. We can, however, still detect bzip2-compressed + * archives and emit a useful message. + */ +static int +init(struct archive *a, const void *buff, size_t n) +{ + (void)a; /* UNUSED */ + (void)buff; /* UNUSED */ + (void)n; /* UNUSED */ + + archive_set_error(a, -1, + "This version of libarchive was compiled without bzip2 support"); + return (ARCHIVE_FATAL); +} + + +#else + /* * Setup the callbacks. */ @@ -264,9 +288,8 @@ read_consume(struct archive *a, size_t n) a->file_position += n; state->read_next += n; if (state->read_next > state->stream.next_out) - errx(1, "Internal error: Request to consume too many " - "bytes from %s decompressor.\n", - a->compression_name); + __archive_errx(1, "Request to consume too many " + "bytes from bzip2 decompressor"); return (n); } diff --git a/lib/libarchive/archive_read_support_compression_compress.c b/lib/libarchive/archive_read_support_compression_compress.c index 2a86c2fa41c1..93683eb89851 100644 --- a/lib/libarchive/archive_read_support_compression_compress.c +++ b/lib/libarchive/archive_read_support_compression_compress.c @@ -67,7 +67,6 @@ #include "archive_platform.h" __FBSDID("$FreeBSD$"); -#include #include #include #include @@ -318,9 +317,8 @@ read_consume(struct archive *a, size_t n) a->file_position += n; state->read_next += n; if (state->read_next > state->next_out) - errx(1, "Internal error: Request to consume too many " - "bytes from %s decompressor.\n", - a->compression_name); + __archive_errx(1, "Request to consume too many " + "bytes from compress decompressor"); return (n); } diff --git a/lib/libarchive/archive_read_support_compression_gzip.c b/lib/libarchive/archive_read_support_compression_gzip.c index 260e1d4b3a25..50da5ba9158b 100644 --- a/lib/libarchive/archive_read_support_compression_gzip.c +++ b/lib/libarchive/archive_read_support_compression_gzip.c @@ -26,22 +26,21 @@ #include "archive_platform.h" -/* Don't compile this if we don't have zlib. */ -#if HAVE_ZLIB_H - __FBSDID("$FreeBSD$"); + #include #include #include #include +#ifdef HAVE_ZLIB_H #include - -#include /* zlib.h is borked, so must precede err.h */ +#endif #include "archive.h" #include "archive_private.h" +#ifdef HAVE_ZLIB_H struct private_data { z_stream stream; unsigned char *uncompressed_buffer; @@ -52,12 +51,15 @@ struct private_data { char header_done; }; -static int bid(const void *, size_t); static int finish(struct archive *); -static int init(struct archive *, const void *, size_t); static ssize_t read_ahead(struct archive *, const void **, size_t); static ssize_t read_consume(struct archive *, size_t); static int drive_decompressor(struct archive *a, struct private_data *); +#endif + +/* These two functions are defined even if we lack zlib. See below. */ +static int bid(const void *, size_t); +static int init(struct archive *, const void *, size_t); int archive_read_support_compression_gzip(struct archive *a) @@ -117,6 +119,29 @@ bid(const void *buff, size_t len) return (bits_checked); } + +#ifndef HAVE_ZLIB_H + +/* + * If we don't have zlib on this system, we can't actually do the + * decompression. We can, however, still detect gzip-compressed + * archives and emit a useful message. + */ +static int +init(struct archive *a, const void *buff, size_t n) +{ + (void)a; /* UNUSED */ + (void)buff; /* UNUSED */ + (void)n; /* UNUSED */ + + archive_set_error(a, -1, + "This version of libarchive was compiled without gzip support"); + return (ARCHIVE_FATAL); +} + + +#else + /* * Setup the callbacks. */ @@ -269,9 +294,8 @@ read_consume(struct archive *a, size_t n) a->file_position += n; state->read_next += n; if (state->read_next > state->stream.next_out) - errx(1, "Internal error: Request to consume too many " - "bytes from %s decompressor.\n", - a->compression_name); + __archive_errx(1, "Request to consume too many " + "bytes from gzip decompressor"); return (n); } diff --git a/lib/libarchive/archive_read_support_format_cpio.c b/lib/libarchive/archive_read_support_format_cpio.c index 03af38fe0a66..b795337221d9 100644 --- a/lib/libarchive/archive_read_support_format_cpio.c +++ b/lib/libarchive/archive_read_support_format_cpio.c @@ -29,7 +29,6 @@ __FBSDID("$FreeBSD$"); #include -#include #include /* #include */ /* See archive_platform.h */ #include diff --git a/lib/libarchive/archive_read_support_format_tar.c b/lib/libarchive/archive_read_support_format_tar.c index e91ddc47b0aa..ecc42ad55f15 100644 --- a/lib/libarchive/archive_read_support_format_tar.c +++ b/lib/libarchive/archive_read_support_format_tar.c @@ -1602,10 +1602,17 @@ UTF8_mbrtowc(wchar_t *pwc, const char *s, size_t n) return ((size_t)-1); } if (pwc != NULL) { - if (wch < WCHAR_MAX) - *pwc = (wchar_t)wch; - else + /* Assign the value to the output; out-of-range values + * just get truncated. */ + *pwc = (wchar_t)wch; +#ifdef WCHAR_MAX + /* + * If platform has WCHAR_MAX, we can do something + * more sensible with out-of-range values. + */ + if (wch >= WCHAR_MAX) *pwc = '?'; +#endif } return (wch == L'\0' ? 0 : len); } diff --git a/lib/libarchive/archive_string.c b/lib/libarchive/archive_string.c index 323b611774f1..0e92836233c6 100644 --- a/lib/libarchive/archive_string.c +++ b/lib/libarchive/archive_string.c @@ -32,10 +32,10 @@ __FBSDID("$FreeBSD$"); * strings while minimizing heap activity. */ -#include #include #include +#include "archive_private.h" #include "archive_string.h" struct archive_string * @@ -68,8 +68,10 @@ __archive_string_ensure(struct archive_string *as, size_t s) while (as->buffer_length < s) as->buffer_length *= 2; as->s = realloc(as->s, as->buffer_length); + /* TODO: Return null instead and fix up all of our callers to + * handle this correctly. */ if (as->s == NULL) - errx(1,"Out of memory"); + __archive_errx(1, "Out of memory"); return (as); } diff --git a/lib/libarchive/archive_string_sprintf.c b/lib/libarchive/archive_string_sprintf.c index c12236900915..174d2f99eed8 100644 --- a/lib/libarchive/archive_string_sprintf.c +++ b/lib/libarchive/archive_string_sprintf.c @@ -34,7 +34,6 @@ __FBSDID("$FreeBSD$"); * the core code, so it cannot easily be omitted.) */ -#include #include #include "archive_string.h" diff --git a/lib/libarchive/archive_util.c b/lib/libarchive/archive_util.c index 2a44498e3c78..77b74ff583d7 100644 --- a/lib/libarchive/archive_util.c +++ b/lib/libarchive/archive_util.c @@ -28,7 +28,7 @@ __FBSDID("$FreeBSD$"); #include - +#include #include #include "archive.h" @@ -149,3 +149,13 @@ archive_set_error(struct archive *a, int error_number, const char *fmt, ...) a->error = a->error_string.s; va_end(ap); } + +void +__archive_errx(int retvalue, const char *msg) +{ + static const char *msg1 = "Fatal Internal Error in libarchive: "; + write(2, msg1, strlen(msg1)); + write(2, msg, strlen(msg)); + write(2, "\n", 1); + exit(retvalue); +}