Commit Graph

17 Commits

Author SHA1 Message Date
FRIGN 11e2d472bf Add *fshut() functions to properly flush file streams
This has been a known issue for a long time. Example:

printf "word" > /dev/full

wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.

However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.

GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.

It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.

the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.

One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.
2015-04-05 09:13:56 +01:00
Hiltjo Posthuma bf90e85f18 col: dont allow col -l 0 2015-03-30 21:49:33 +02:00
Hiltjo Posthuma b1fcce32e2 col: exit status 1 on usage 2015-03-30 21:49:04 +02:00
Hiltjo Posthuma 24c6df3d73 col: buffer 128 lines by default 2015-03-30 21:48:26 +02:00
FRIGN 521f324319 Audit col(1), add UTF-8-support
Nothing special here, only renaming of variables and adding the
Rune-utility-functions.
Also, I refactored the manpage.
2015-03-22 21:45:32 +01:00
FRIGN 833c2aebb4 Remove mallocarray(...) and use reallocarray(NULL, ...)
After a short correspondence with Otto Moerbeek it turned out
mallocarray() is only in the OpenBSD-Kernel, because the kernel-
malloc doesn't have realloc.
Userspace applications should rather use reallocarray with an
explicit NULL-pointer.

Assuming reallocarray() will become available in c-stdlibs in the
next few years, we nip mallocarray() in the bud to allow an easy
transition to a system-provided version when the day comes.
2015-03-11 10:50:18 +01:00
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
FRIGN 3b825735d8 Implement reallocarray()
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
2015-03-10 21:23:36 +01:00
FRIGN 4c4e5a3eb2 Remove trailing whitespace in col.c 2015-03-04 18:48:44 +01:00
sin 5d0abb92aa Staticise globals in col(1) 2015-03-03 13:37:15 +00:00
sin e6a5c736cb Add license header in col(1) 2015-03-03 13:36:25 +00:00
Roberto E. Vargas Caballero 443de0a859 Add col command
col is used to display troff documents in ttys, removing the reverse
line feeds generated by .2C in ms. This implementation keeps the limit
of 256 lines of 800 characteres of the original implementation.
2015-03-03 13:35:42 +00:00
sin 5197affac5 Remove col(1)
Not quite necessary to have this in sbase at the moment.  We can do
a clean implementation when required.

This implementation also has some bugs that they have been fixed
in OpenBSD -current but I am too lazy to backport (we also had local
changes to col(1)).

printf(1) as imported from OpenBSD will stay for now because I need
it for booting my system.
2014-11-20 13:57:52 +00:00
sin ff93350289 Fix col(1) -f
patch taken from openbsd.

Ingo Schwarze says:

If you call the col(1) utility with the -f option, permitting forward
half-line feeds in the output stream, and the input stream actually
contains half-line feeds in either direction, you end up with corrupt
output, containing meaningless escape-digitnine sequences instead of
the required escape-tab sequences.

   $ hexdump -C half.txt
  00000000  61 1b 09 62 1b 09 63 0a                    |a..b..c.|
  00000008
   $ col -f < half.txt | hexdump -C
  00000000  61 1b 39 0d 20 62 1b 39  0d 20 20 63 0a    |a.9. b.9.  c.|
  0000000d

Note how the third character changes from 0x09 to 0x39.

OK to commit the following fix?  Don't worry, it isn't dangerous,
it only changes two *bits*, only a quarter of a byte.

The bug was introduced by the original author, Michael Rendell,
and committed by Keith Bostic on May 22, 1990 (SCCS rev. 5.1).

The following operating systems are affected:

 * 4.3BSD Reno, BSD Net/2, 4.4BSD, 4.4BSD Lite1, 4.4BSD Lite2
 * All versions of 386BSD, NetBSD, OpenBSD, FreeBSD and DragonFly
 * All versions of Debian GNU/Linux and probably many other Linuxes
2014-10-17 21:14:00 +01:00
sin 41d78c398b Staticise symbols 2014-10-16 12:30:52 +01:00
sin 0c5b7b9155 Stop using EXIT_{SUCCESS,FAILURE} 2014-10-02 23:46:59 +01:00
sin a48b850d3f Import col(1) from OpenBSD with minor modifications
Remove getopt, use arg.h.
2014-09-29 19:52:31 +01:00