Commit Graph

16 Commits

Author SHA1 Message Date
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
Evan Gates
d21a958d88 bug and style fixes in find
1) don't mix declarations and code (leave recursion alone for now as I
plan on changing/using recurse)
2) change **argv to *argv[]
3) check for error on fork()
2015-03-06 18:31:14 +00:00
Evan Gates
3eed1fced6 a bunch of cleanup 2015-02-25 10:11:55 +00:00
Evan Gates
e427364a28 use struct literal instead of filling each field manually 2015-02-25 10:11:47 +00:00
Evan Gates
4671b4c974 no need for array of function pointers for comparisons, just use the necessary function pointer itself 2015-02-25 10:11:35 +00:00
Evan Gates
49d80b46bb add space for END token 2015-02-24 10:23:18 +00:00
Evan Gates
91bb8ed7d6 remove extra get_n_arg declarations (copy pasta) 2015-02-24 10:23:17 +00:00
Evan Gates
6abce61877 insert implicit -a after primary before ! 2015-02-21 09:25:05 +00:00
Evan Gates
639a74a537 find: Change execv to execvp
- Make globals static
- Fix a comment
- Change some data types
- Rearrange struct members from largest to smallest
  (no affect due to small structs, good practice)
2015-02-21 09:24:31 +00:00
sin
b08bb6aad6 Use st_mtime as opposed to st_mtim.tv_sec
This is more portable and fixes a build issue on NetBSD.  We ignore
nanoseconds in this case so there's no functional difference.
2015-02-20 15:47:50 +00:00
Roberto E. Vargas Caballero
b8fbaa9b0d Remove bit fields
Before removing bit fields:

$ size find

   text	   data	    bss	    dec	    hex	filename

  16751	    968	     48	  17767	   4567	find

After removing bit fields:
$ size find
   text    data     bss     dec     hex filename
  16527     968      68   17563    449b find

This is an example where bit fields uses more memory
than integers or char. There is going to be only one
gflags struct, so the waste in instructions is bigger
than the space saved by bit fields. In the case of Permarg,
Sizearg, Execarg there is only one bit field, so at least
one unsigned is used, so there is no any gain.
2015-02-20 13:46:56 +00:00
Hiltjo Posthuma
6c7ff5fda5 code-style: unindent one level of switch 2015-02-20 13:29:38 +01:00
sin
028b0c206f Fix style
This broke sbase-box because it assumes that main(...) starts on
a separate line.
2015-02-20 10:24:11 +00:00
Evan Gates
76e6aacd60 Add initial find(1) implementation
No manpage yet.
2015-02-20 10:17:16 +00:00