Skip to content

Commit

Permalink
Pull up revision 1.3 (requested by mlelstv in ticket #527):
Browse files Browse the repository at this point in the history
PR/29806: Michael Van Elst: Off by one in code.
While I am there:
        - factor out the binary test to an inline function.
        - use size_t where appropriate.
        - check for <= 0 in gzread; it returns -1 on error.
  • Loading branch information
Matthias Scheler authored and Matthias Scheler committed Jul 3, 2005
1 parent 0b347db commit b0cb273
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions binary.c
@@ -1,4 +1,4 @@
/* $NetBSD: binary.c,v 1.6 2003/11/08 20:08:50 cjep Exp $ */
/* $NetBSD$ */

/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
Expand Down Expand Up @@ -29,7 +29,7 @@

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: binary.c,v 1.6 2003/11/08 20:08:50 cjep Exp $");
__RCSID("$NetBSD$");
#endif /* not lint */

#include <ctype.h>
Expand All @@ -40,21 +40,26 @@ __RCSID("$NetBSD: binary.c,v 1.6 2003/11/08 20:08:50 cjep Exp $");

#define BUFFER_SIZE 128

static inline int
okchar(unsigned char c)
{
return isprint(c) || isspace(c) || c == line_endchar;
}

int
bin_file(FILE *f)
{
char buf[BUFFER_SIZE];
int i, m;
unsigned char buf[BUFFER_SIZE];
size_t i, m;

if (fseek(f, 0L, SEEK_SET) == -1)
return 0;

if ((m = (int)fread(buf, 1, BUFFER_SIZE, f)) == 0)
if ((m = fread(buf, 1, BUFFER_SIZE, f)) == 0)
return 0;

for (i = 0; i < m; i++)
if (!isprint(buf[i]) && !isspace(buf[i]) &&
buf[i] != line_endchar)
if (!okchar(buf[i]))
return 1;

rewind(f);
Expand All @@ -64,18 +69,17 @@ bin_file(FILE *f)
int
gzbin_file(gzFile *f)
{
char buf[BUFFER_SIZE];
unsigned char buf[BUFFER_SIZE];
int i, m;

if (gzseek(f, 0L, SEEK_SET) == -1)
return 0;

if ((m = gzread(f, buf, BUFFER_SIZE)) == 0)
if ((m = gzread(f, buf, BUFFER_SIZE)) <= 0)
return 0;

for (i = 0; i < m; i++)
if (!isprint(buf[i]) && !isspace(buf[i]) &&
buf[i] != line_endchar)
if (!okchar(buf[i]))
return 1;

gzrewind(f);
Expand All @@ -85,11 +89,10 @@ gzbin_file(gzFile *f)
int
mmbin_file(mmf_t *f)
{
int i;
size_t i;
/* XXX knows too much about mmf internals */
for (i = 0; i < BUFFER_SIZE && i < f->len - 1; i++)
if (!isprint(f->base[i]) && !isspace(f->base[i]) &&
f->base[i] != line_endchar)
for (i = 0; i < BUFFER_SIZE && i < f->len; i++)
if (!okchar(f->base[i]))
return 1;
mmrewind(f);
return 0;
Expand Down

0 comments on commit b0cb273

Please sign in to comment.