From 20806454f8348d631df2c94208febe56b00e0adb Mon Sep 17 00:00:00 2001 From: didg Date: Wed, 27 Jan 2010 21:27:53 +0000 Subject: [PATCH] megatron, fix errors introduced in Sun Feb 6 10:16:00 2005 commit, 'replace a wrong test for protecting against buffers underflow by ... a buffer underflow, always' . --- bin/megatron/asingle.c | 14 +++++++------- bin/megatron/asingle.h | 4 ++-- bin/megatron/hqx.c | 31 +++++++++++-------------------- bin/megatron/hqx.h | 8 ++++---- bin/megatron/macbin.c | 25 ++++++++++--------------- bin/megatron/macbin.h | 6 +++--- bin/megatron/megatron.c | 10 +++++----- bin/megatron/nad.c | 12 ++++++------ bin/megatron/nad.h | 6 +++--- 9 files changed, 51 insertions(+), 65 deletions(-) diff --git a/bin/megatron/asingle.c b/bin/megatron/asingle.c index 4fb9a3ea..60c754f0 100644 --- a/bin/megatron/asingle.c +++ b/bin/megatron/asingle.c @@ -1,5 +1,5 @@ /* - * $Id: asingle.c,v 1.13 2009-10-14 01:38:28 didg Exp $ + * $Id: asingle.c,v 1.14 2010-01-27 21:27:53 didg Exp $ */ #ifdef HAVE_CONFIG_H @@ -389,12 +389,12 @@ int single_header_test(void) * */ -int single_read( int fork, char *buffer, u_int32_t length) +ssize_t single_read( int fork, char *buffer, size_t length) { u_int32_t entry_id; char *buf_ptr; - int readlen; - int cc = 1; + size_t readlen; + ssize_t cc = 1; off_t pos; switch ( fork ) { @@ -409,9 +409,9 @@ int single_read( int fork, char *buffer, u_int32_t length) break; } - if (single.entry[entry_id].ade_len > length) { - fprintf(stderr, "single_read: Trying to read past end of fork!, length %d, ade_len == %u\n", length, single.entry[entry_id].ade_len); - return single.entry[entry_id].ade_len; + if (single.entry[entry_id].ade_len > 0x7FFFFFFF) { + fprintf(stderr, "single_read: Trying to read past end of fork!, ade_len == %u\n", single.entry[entry_id].ade_len); + return -1; } if ( single.entry[ entry_id ].ade_len == 0 ) { if ( fork == DATA ) { diff --git a/bin/megatron/asingle.h b/bin/megatron/asingle.h index 557ba9d0..c041ccaa 100644 --- a/bin/megatron/asingle.h +++ b/bin/megatron/asingle.h @@ -1,5 +1,5 @@ /* - * $Id: asingle.h,v 1.3 2005-04-28 20:49:19 bfernhomberg Exp $ + * $Id: asingle.h,v 1.4 2010-01-27 21:27:53 didg Exp $ */ #ifndef _ASINGLE_H @@ -12,6 +12,6 @@ int single_open(char *singlefile, int flags, struct FHeader *fh, int options); int single_close(int readflag); int single_header_read(struct FHeader *fh, int version); int single_header_test(void); -int single_read(int fork, char *buffer, u_int32_t length); +ssize_t single_read(int fork, char *buffer, size_t length); #endif /* _ASINGLE_H */ diff --git a/bin/megatron/hqx.c b/bin/megatron/hqx.c index 43db208d..117c9dd6 100644 --- a/bin/megatron/hqx.c +++ b/bin/megatron/hqx.c @@ -1,5 +1,5 @@ /* - * $Id: hqx.c,v 1.17 2009-10-14 02:24:04 didg Exp $ + * $Id: hqx.c,v 1.18 2010-01-27 21:27:53 didg Exp $ */ #ifdef HAVE_CONFIG_H @@ -66,15 +66,6 @@ #define BHH_CRCSIZ 2 #define BHH_HEADSIZ 21 -/* Forward declarations. - */ -int skip_junk(int line); -int hqx_close(int keepflag); -int hqx_header_read(struct FHeader *fh); -int hqx_header_write(struct FHeader *fh); -int hqx_7tobin(char *outbuf, int datalen); -int hqx7_fill(u_char *hqx7_ptr); - #if HEXOUTPUT FILE *rawhex, *expandhex; #endif /* HEXOUTPUT */ @@ -183,11 +174,11 @@ int hqx_close(int keepflag) * return zero and no more than that. */ -int hqx_read(int fork, char *buffer, int length) +ssize_t hqx_read(int fork, char *buffer, size_t length) { u_short storedcrc; - int readlen; - int cc; + size_t readlen; + size_t cc; #if DEBUG >= 3 { @@ -199,9 +190,9 @@ int hqx_read(int fork, char *buffer, int length) fprintf( stderr, "hqx_read: remaining length is %d\n", hqx.forklen[fork] ); #endif /* DEBUG >= 3 */ - if (hqx.forklen[fork] > length) { - fprintf(stderr, "This should never happen, dude! length %d, fork length == %u\n", length, hqx.forklen[fork]); - return hqx.forklen[fork]; + if (hqx.forklen[fork] > 0x7FFFFFFF) { + fprintf(stderr, "This should never happen, dude!, fork length == %u\n", hqx.forklen[fork]); + return -1; } if ( hqx.forklen[ fork ] == 0 ) { @@ -395,10 +386,10 @@ int hqx_header_write(struct FHeader *fh _U_) * it sets the pointers to the hqx7 buffer up to point to the valid data. */ -int hqx7_fill(u_char *hqx7_ptr) +ssize_t hqx7_fill(u_char *hqx7_ptr) { - int cc; - int cs; + ssize_t cc; + size_t cs; cs = hqx7_ptr - hqx7_buf; if ( cs >= sizeof( hqx7_buf )) return( -1 ); @@ -566,7 +557,7 @@ int skip_junk(int line) * file is reached. */ -int hqx_7tobin( char *outbuf, int datalen) +size_t hqx_7tobin( char *outbuf, size_t datalen) { static u_char hqx8[3]; static int hqx8i; diff --git a/bin/megatron/hqx.h b/bin/megatron/hqx.h index 73fbdeae..38730d05 100644 --- a/bin/megatron/hqx.h +++ b/bin/megatron/hqx.h @@ -1,5 +1,5 @@ /* - * $Id: hqx.h,v 1.2 2001-06-29 14:14:46 rufustfirefly Exp $ + * $Id: hqx.h,v 1.3 2010-01-27 21:27:53 didg Exp $ */ #ifndef _HQX_H @@ -11,10 +11,10 @@ struct FHeader; int skip_junk(int line); int hqx_open(char *hqxfile, int flags, struct FHeader *fh, int options); int hqx_close(int keepflag); -int hqx_read(int fork, char *buffer, int length); +ssize_t hqx_read(int fork, char *buffer, size_t length); int hqx_header_read(struct FHeader *fh); int hqx_header_write(struct FHeader *fh); -int hqx_7tobin(char *outbuf, int datalen); -int hqx7_fill(u_char *hqx7_ptr); +size_t hqx_7tobin(char *outbuf, size_t datalen); +ssize_t hqx7_fill(u_char *hqx7_ptr); #endif /* _HQX_H */ diff --git a/bin/megatron/macbin.c b/bin/megatron/macbin.c index 1b2dd197..349a7e1e 100644 --- a/bin/megatron/macbin.c +++ b/bin/megatron/macbin.c @@ -1,5 +1,5 @@ /* - * $Id: macbin.c,v 1.14 2009-10-14 01:38:28 didg Exp $ + * $Id: macbin.c,v 1.15 2010-01-27 21:27:53 didg Exp $ */ #ifdef HAVE_CONFIG_H @@ -167,11 +167,11 @@ int bin_close(int keepflag) * return zero and no more than that. */ -int bin_read( int fork, char *buffer, int length) +ssize_t bin_read( int fork, char *buffer, size_t length) { char *buf_ptr; - int readlen; - int cc = 1; + size_t readlen; + ssize_t cc = 1; off_t pos; #if DEBUG >= 3 @@ -179,9 +179,9 @@ int bin_read( int fork, char *buffer, int length) fprintf( stderr, "bin_read: remaining length is %d\n", bin.forklen[fork] ); #endif /* DEBUG >= 3 */ - if (bin.forklen[fork] > length) { - fprintf(stderr, "This should never happen, dude! length %d, fork length == %u\n", length, bin.forklen[fork]); - return bin.forklen[fork]; + if (bin.forklen[fork] > 0x7FFFFFFF) { + fprintf(stderr, "This should never happen, dude! fork length == %u\n", bin.forklen[fork]); + return -1; } if ( bin.forklen[ fork ] == 0 ) { @@ -236,11 +236,11 @@ int bin_read( int fork, char *buffer, int length) * bin_write */ -int bin_write(int fork, char *buffer, int length) +ssize_t bin_write(int fork, char *buffer, size_t length) { char *buf_ptr; - int writelen; - int cc = 0; + size_t writelen; + ssize_t cc = 0; off_t pos; u_char padchar = 0x7f; /* Not sure why, but it seems this must be 0x7f to match @@ -278,11 +278,6 @@ int bin_write(int fork, char *buffer, int length) return( cc ); } - if (length > bin.forklen[fork]) { - fprintf(stderr, "This should never happen, dude! length %d, fork length %u\n", length, bin.forklen[fork]); - return bin.forklen[fork]; - } - bin.forklen[fork] -= length; /* diff --git a/bin/megatron/macbin.h b/bin/megatron/macbin.h index e9ec2834..d216b1b9 100644 --- a/bin/megatron/macbin.h +++ b/bin/megatron/macbin.h @@ -1,5 +1,5 @@ /* - * $Id: macbin.h,v 1.3 2009-10-13 22:55:36 didg Exp $ + * $Id: macbin.h,v 1.4 2010-01-27 21:27:53 didg Exp $ */ #ifndef _MACBIN_H @@ -10,8 +10,8 @@ struct FHeader; int bin_open(char *binfile, int flags, struct FHeader *fh, int options); int bin_close(int keepflag); -int bin_read(int fork, char *buffer, int length); -int bin_write(int fork, char *buffer, int length); +ssize_t bin_read(int fork, char *buffer, size_t length); +ssize_t bin_write(int fork, char *buffer, size_t length); int bin_header_read(struct FHeader *fh, int revision); int bin_header_write(struct FHeader *fh); int test_header(void); diff --git a/bin/megatron/megatron.c b/bin/megatron/megatron.c index 583465a9..1dc83d77 100644 --- a/bin/megatron/megatron.c +++ b/bin/megatron/megatron.c @@ -1,5 +1,5 @@ /* - * $Id: megatron.c,v 1.13 2009-10-14 02:24:04 didg Exp $ + * $Id: megatron.c,v 1.14 2010-01-27 21:27:53 didg Exp $ */ #ifdef HAVE_CONFIG_H @@ -61,7 +61,7 @@ static int from_open(int un, char *file, struct FHeader *fh, int flags) } } -static int from_read(int un, int fork, char *buf, int len) +static ssize_t from_read(int un, int fork, char *buf, size_t len) { switch ( un ) { case MEGATRON : @@ -127,7 +127,7 @@ static int to_open(int to, char *file, struct FHeader *fh, int flags) } } -static int to_write(int to, int fork, int bufc) +static ssize_t to_write(int to, int fork, size_t bufc) { switch ( to ) { case MEGATRON : @@ -171,9 +171,9 @@ static int megatron( char *path, int module, char *newname, int flags) { struct stat st; struct FHeader fh; - int bufc; + ssize_t bufc; int fork; - unsigned int forkred; + size_t forkred; /* * If the source file is not stdin, make sure it exists and diff --git a/bin/megatron/nad.c b/bin/megatron/nad.c index 250f51ac..92be3541 100644 --- a/bin/megatron/nad.c +++ b/bin/megatron/nad.c @@ -1,5 +1,5 @@ /* - * $Id: nad.c,v 1.17 2009-10-14 01:38:28 didg Exp $ + * $Id: nad.c,v 1.18 2010-01-27 21:27:53 didg Exp $ */ #ifdef HAVE_CONFIG_H @@ -691,9 +691,9 @@ int nad_header_write(struct FHeader *fh) static int forkeid[] = { ADEID_DFORK, ADEID_RFORK }; -int nad_read(int fork, char *forkbuf, int bufc) +ssize_t nad_read(int fork, char *forkbuf, size_t bufc) { - int cc = 0; + ssize_t cc = 0; #if DEBUG fprintf( stderr, "Entering nad_read\n" ); @@ -713,11 +713,11 @@ int nad_read(int fork, char *forkbuf, int bufc) return( cc ); } -int nad_write(int fork, char *forkbuf, int bufc) +ssize_t nad_write(int fork, char *forkbuf, size_t bufc) { char *buf_ptr; - int writelen; - int cc = 0; + size_t writelen; + ssize_t cc = 0; #if DEBUG fprintf( stderr, "Entering nad_write\n" ); diff --git a/bin/megatron/nad.h b/bin/megatron/nad.h index b4b43ec0..9fa85684 100644 --- a/bin/megatron/nad.h +++ b/bin/megatron/nad.h @@ -1,5 +1,5 @@ /* - * $Id: nad.h,v 1.4 2005-04-28 20:49:20 bfernhomberg Exp $ + * $Id: nad.h,v 1.5 2010-01-27 21:27:53 didg Exp $ */ #ifndef _NAD_H @@ -11,8 +11,8 @@ struct FHeader; int nad_open(char *path, int openflags, struct FHeader *fh, int options); int nad_header_read(struct FHeader *fh); int nad_header_write(struct FHeader *fh); -int nad_read(int fork, char *forkbuf, int bufc); -int nad_write(int fork, char *forkbuf, int bufc); +ssize_t nad_read(int fork, char *forkbuf, size_t bufc); +ssize_t nad_write(int fork, char *forkbuf, size_t bufc); int nad_close(int status); void select_charset(int options); -- 2.39.2