Stateless and I stumbled upon this issue while discussing the semantics of read, accepting a size_t but only being able to return ssize_t, effectively lacking the ability to report successful reads > SSIZE_MAX. The discussion went along and we came to the topic of input-based memory allocations. Basically, it was possible for the argument to a memory-allocation-function to overflow, leading to a segfault later. The OpenBSD-guys came up with the ingenious reallocarray-function, and I implemented it as ereallocarray, which automatically returns on error. Read more about it here[0]. A simple testcase is this (courtesy to stateless): $ sbase-strings -n (2^(32|64) / 4) This will segfault before this patch and properly return an OOM- situation afterwards (thanks to the overflow-check in reallocarray). [0]: http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/calloc.3
93 lines
1.5 KiB
C
93 lines
1.5 KiB
C
/* See LICENSE file for copyright and license details. */
|
|
#include <limits.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "utf.h"
|
|
#include "util.h"
|
|
|
|
static char *format = "";
|
|
|
|
static void
|
|
strings(FILE *fp, const char *fname, size_t len)
|
|
{
|
|
Rune r, *rbuf = NULL;
|
|
size_t i, bread;
|
|
off_t off;
|
|
|
|
rbuf = ereallocarray(rbuf, len, sizeof(*rbuf));
|
|
|
|
for (off = 0, i = 0; (bread = efgetrune(&r, fp, fname)); ) {
|
|
off += bread;
|
|
if (r == Runeerror)
|
|
continue;
|
|
if (!isprintrune(r)) {
|
|
if (i > len)
|
|
putchar('\n');
|
|
i = 0;
|
|
continue;
|
|
}
|
|
if (i < len) {
|
|
rbuf[i++] = r;
|
|
continue;
|
|
} else if (i > len) {
|
|
efputrune(&r, stdout, "<stdout>");
|
|
continue;
|
|
}
|
|
printf(format, (long)off - i);
|
|
for (i = 0; i < len; i++)
|
|
efputrune(rbuf + i, stdout, "<stdout>");
|
|
efputrune(&r, stdout, "<stdout>");
|
|
i++;
|
|
}
|
|
free(rbuf);
|
|
}
|
|
|
|
static void
|
|
usage(void)
|
|
{
|
|
eprintf("usage: %s [-a] [-n num] [-t format] [file ...]\n", argv0);
|
|
}
|
|
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
FILE *fp;
|
|
size_t len = 4;
|
|
int ret = 0;
|
|
char f;
|
|
|
|
ARGBEGIN {
|
|
case 'a':
|
|
break;
|
|
case 'n':
|
|
len = estrtonum(EARGF(usage()), 1, LLONG_MAX);
|
|
break;
|
|
case 't':
|
|
format = estrdup("%8l#: ");
|
|
f = *EARGF(usage());
|
|
if (f == 'd' || f == 'o' || f == 'x')
|
|
format[3] = f;
|
|
else
|
|
usage();
|
|
break;
|
|
default:
|
|
usage();
|
|
} ARGEND;
|
|
|
|
if (argc == 0) {
|
|
strings(stdin, "<stdin>", len);
|
|
} else {
|
|
for (; argc > 0; argc--, argv++) {
|
|
if (!(fp = fopen(argv[0], "r"))) {
|
|
weprintf("fopen %s:", argv[0]);
|
|
ret = 1;
|
|
continue;
|
|
}
|
|
strings(fp, argv[0], len);
|
|
fclose(fp);
|
|
}
|
|
}
|
|
return ret;
|
|
}
|