sbase/strings.c
FRIGN 3c33abc520 Implement mallocarray()
A function used only in the OpenBSD-Kernel as of now, but it surely
provides a helpful interface when you just don't want to make sure
the incoming pointer to erealloc() is really NULL so it behaves
like malloc, making it a bit more safer.

Talking about *allocarray(): It's definitely a major step in code-
hardening. Especially as a system administrator, you should be
able to trust your core tools without having to worry about segfaults
like this, which can easily lead to privilege escalation.

How do the GNU coreutils handle this?
$ strings -n 4611686018427387903
strings: invalid minimum string length -1
$ strings -n 4611686018427387904
strings: invalid minimum string length 0

They silently overflow...

In comparison, sbase:

$ strings -n 4611686018427387903
mallocarray: out of memory
$ strings -n 4611686018427387904
mallocarray: out of memory

The first out of memory is actually a true OOM returned by malloc,
whereas the second one is a detected overflow, which is not marked
in a special way.
Now tell me which diagnostic error-messages are easier to understand.
2015-03-10 22:19:19 +01:00

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;
size_t i, bread;
off_t off;
rbuf = emallocarray(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;
}