sbase/paste.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

136 lines
2.3 KiB
C

/* See LICENSE file for copyright and license details. */
#include <stdlib.h>
#include <string.h>
#include "utf.h"
#include "util.h"
struct fdescr {
FILE *fp;
const char *name;
};
static void
sequential(struct fdescr *dsc, int fdescrlen, Rune *delim, size_t delimlen)
{
Rune c, last;
size_t i, d;
for (i = 0; i < fdescrlen; i++) {
d = 0;
last = 0;
while (efgetrune(&c, dsc[i].fp, dsc[i].name)) {
if (last == '\n') {
if (delim[d] != '\0')
efputrune(&delim[d], stdout, "<stdout>");
d = (d + 1) % delimlen;
}
if (c != '\n')
efputrune(&c, stdout, "<stdout>");
last = c;
}
if (last == '\n')
efputrune(&last, stdout, "<stdout>");
}
}
static void
parallel(struct fdescr *dsc, int fdescrlen, Rune *delim, size_t delimlen)
{
Rune c, d;
size_t i, m;
ssize_t last;
nextline:
last = -1;
for (i = 0; i < fdescrlen; i++) {
d = delim[i % delimlen];
c = 0;
for (; efgetrune(&c, dsc[i].fp, dsc[i].name) ;) {
for (m = last + 1; m < i; m++)
efputrune(&(delim[m % delimlen]), stdout, "<stdout>");
last = i;
if (c == '\n') {
if (i != fdescrlen - 1)
c = d;
efputrune(&c, stdout, "<stdout>");
break;
}
efputrune(&c, stdout, "<stdout>");
}
if (c == 0 && last != -1) {
if (i == fdescrlen - 1)
putchar('\n');
else
efputrune(&d, stdout, "<stdout>");
last++;
}
}
if (last != -1)
goto nextline;
}
static void
usage(void)
{
eprintf("usage: %s [-s] [-d list] file ...\n", argv0);
}
int
main(int argc, char *argv[])
{
struct fdescr *dsc;
Rune *delim;
size_t i, len;
int seq = 0;
char *adelim = "\t";
ARGBEGIN {
case 's':
seq = 1;
break;
case 'd':
adelim = EARGF(usage());
break;
default:
usage();
} ARGEND;
if (argc == 0)
usage();
/* populate delimiters */
unescape(adelim);
delim = emallocarray(utflen(adelim) + 1, sizeof(*delim));
len = utftorunestr(adelim, delim);
/* populate file list */
dsc = emallocarray(argc, sizeof(*dsc));
for (i = 0; i < argc; i++) {
if (strcmp(argv[i], "-") == 0)
dsc[i].fp = stdin;
else
dsc[i].fp = fopen(argv[i], "r");
if (!dsc[i].fp)
eprintf("fopen %s:", argv[i]);
dsc[i].name = argv[i];
}
if (seq)
sequential(dsc, argc, delim, len);
else
parallel(dsc, argc, delim, len);
for (i = 0; i < argc; i++)
fclose(dsc[i].fp);
return 0;
}