Commit Graph

13 Commits

Author SHA1 Message Date
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