Censorship, Freedom of Speech, Privacy

Please join the Electronic Frontier Foundation ( EFF.org ) and the fight for your rights on the Internet.

I have not received any National Security Letter.

HOME Software Lotus Cars DWARF Kindle eeepc PALM RPM Building

graphic with lotus europa and lotus elan

David A's C Software

C Programming

All the code here is provided in C source and is free software. Compile it yourself. Enjoy!

Testing Malloc Failure

"Code Testing Through Fault Injection" in ":login;" magazine (December, 2014. Usenix.org) by Peter Gutmann offered a simple example by an unnamed friend: instrument malloc() so on call N it returns NULL. Try with N from 0 to some higher number. On any system using dynamically-loaded libc it is easy to do. Here we set it for current Linux libc. Run your chosen executable and see how it fares.

Here is the basic C source. Using a script (example below) link this into the executable you want to test.

/*  fakemalloc.in */
/*  script below modifies this into a temporary fakemalloc.c */
static unsigned count;
extern void * __libc_malloc();

void *malloc(size_t len)
  /*  Perhaps the test should be count >= FAILCOUNT  ??? */
  if (count == FAILCOUNT) {
    return 0;
  return __libc_malloc(len);

Here we build the executable to be tested. The executable to test can be anything. Naturally you have some application in mind. Normally you would modify the link step of your executable creation . Here we assume you use make to build the executable under test. Lets say the executable is named mycode

# This is a version of a link line presumed to be in Makefile
# Fairly typical use of make.
# It is essential that fakemalloc.o is linked in before
# the standard libraries, but its exact location on the link
# line is normally not critical.
   # This line new, for test
   cc -g -c fakemalloc.c
   # This line adds fakemalloc.o
   cc -g $(CFLAGS) mycode.o fakemalloc.o -o mycode $(OBJS) $(LDFLAGS) $(LIBS)

So with that as background, we move to the example test script.


while [ $ct -lt  500 ]
  echo '===== START TEST'
  rm -f core
  # rm  $targ so the test executable gets rebuild.
  rm -f $targ
  sed -e "s/FAILCOUNT/$ct/"  < fakemalloc.in > fakemalloc.c
  echo TEST $ct
  # This is just so you can see the sed worked.
  grep if fakemalloc.c
  # Next line presumably does the compile of fakemalloc.c 
  # and the build of mycode.
  $targ  "place args here" > junk.x
  # to see the output on stdout.
  cat junk.x
  if [ -f core ]
    echo "CORE FILE EXISTS, test" > $ct
  rm -f core
  echo '===== END TEST'
  ct=`expr $ct + 1`

I hope you find this useful for your testing.


Important C Documents

Some key writings on C for C programmers (from Henry Spencer, Dennis Richie, and others) is at http://www.lysator.liu.se/c/ C programmers will enjoy this. Take a look.


POSIX Signals, C and C++

Occasionally folks are unaware of or misunderstand the rules on writing POSIX signal handlers in C and C++. So I've written a brief overview of POSIX signal handling pitfalls. Corrections and suggestions welcome.


Hex Dump Utility

When viewing the binary representation of a file I prefer to use a format I first saw in a utility provided by IBM in OS360 in 1964. So I wrote C code I call hxdump that prepares output in a similar format. Download the hxdump.c C source. To compile it cc -g hxdump.c -o hxdump should suffice. The source has always said there are no restrictions on its use.


Binary Patch Utility

Sometimes (rarely) one has a file one wants to patch directly, rather than use an editor. Years ago I wrote (and made available via ftp) a program I call binpatch. Download the binpatch.c C source. To compile it cc -g binpatch.c -o binpatch should suffice. The source has always said there are no restrictions on its use.


Palm Data Extractor

See the page describing a palm extractor in C. It is a way to get data out of a palm data file (palm provides no way to do this!).


Tsearch Search Example In C

A standard based search is quite useful, but tsearch has its peculiarities, so perhaps an example is of interest. See the page describing a tsearch example in C.


Tsearch Implementation Source in C

I wanted a set of search calls that I could use everywhere. Tsearch, for example, is widely but not universally available. And the functionality to completely and easily take down a tsearch tree is even less available. So I implemented several tree search algorithms and added a hash algorithm and placed the source to all of them in the libdwarf source (see dwarf.html in in the tsearch directory.

The implemented tree searches are of binary tree, binary tree with Eppinger delete, balanced tree, and red-black tree, The hash search uses the same interfaces. To avoid conflicts with your standard library, the function names are prefixed with dwarf_. The implemented functions (for every version) are dwarf_tsearch(), dwarf_tfind(),dwarf_tdelete(), dwarf_tdestroy(), dwarf_twalk(), and dwarf_tdump().