Cutting Boards: Wood or Plastic?

It’s natural to assume that plastic would not harbor as much bacteria compared with wooden cutting boards if you ask a normal person, but guess what, that’s not true.

Plastic Cutting Boards

Plastic is synthetic and nonporous, which means bacteria can’t get into the plastic, and also that it’s not something that bacteria can thrive off of. When cleaned thoroughly with hot water and soap, it’s basically void of bacteria.

Wooden Cutting Boards

Wood, on the other hand is organic and porous. It’s natural to assume that it’s a good material for harboring bacteria.

Then Why are Wooden Cutting Boards Better?

Because wooden cutting boards are porous by nature, after properly cleaning the surface with soap and hot water, any leftover bacteria is absorbed into the cutting board within several minutes. As long as the surface is clean, who cares that you’re cutting on top of something that harbors bacteria?

Plastic cutting boards on the other hand, are hard to clean when there are grooves made by cutting. Within each of these grooves, bacteria can thrive and get on your salad, fruits, or vegetables if you previously placed raw meat on your board.

For more technical details, take a look at a research done on this topic here: “Cutting Boards of Plastic and Wood Contaminated Experimentally with Bacteria”.

How to Override C Functions on UNIX based OS’s

This example will guide on how to override C Functions in either linux or solaris by wrapping the calls to malloc and free.

This article is solely meant to be educational and by no means should be used to fix poorly written code.

Let’s create a file called malloc_guard.c:

First things first. Here are the required #include’s and #defines:

#define _GNU_SOURCE

#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>

#define MALLOC_PAD 50

In order for some macro’s that we will be using to be declared, _GNU_SOURCE needs to be defined. We will be using functions that are found in the dlfcn library to find the address to malloc( ) itself. MALLOC_PAD will be the value of how many bytes to pad malloc( ) calls with.

Now we declare a function pointer that will later point to malloc( ):

static void (*orig_malloc)(size_t size) = NULL;

Here is the malloc( ) function we create:

void* malloc(size_t size)
{
  void *allocated = NULL;

  if(!orig_malloc)
{
fprintf(stderr, "malloc_guard:: wrapping malloc\n");
fflush(stderr);
*(void **) (&orig_malloc) = dlsym(RTLD_NEXT, "malloc");
}

allocated = orig_malloc(size+MALLOC_PAD);
if(!allocated)
{
fprintf(stderr, "malloc_guard:: --MALLOC RETURNED NULL PTR--\n");
fflush(stderr);
}

  return allocated;
}

Alright, there it is, a padded malloc call, suitable for fixing some bad implementation.

The next step is to make sure this malloc( ) is called in place of the standard malloc( ) call. There are two ways to do this, dynamically and statically.

In order to compile this into a dynamic library we use:
gcc malloc_guard.c -fPIC -shared -o malloc_guard.so

Once you have the malloc_guard.so library, you can set your environment to look for the malloc in this library before it looks in glibc by defining LD_PRELOAD.

(ba)sh shell:
export LD_PRELOAD=$PWD/malloc_guard.so

You can verify that any program you call is using your malloc by seeing “malloc_guard:: wrapping malloc” print out to stderr.

To have your malloc( ) be called every time you run a specific program, just compile it into a static library object file and link it in at compile time.

How is this useful? This code snippet can be taken and be revised to pinpoint bugs in code.  Library functions other than malloc and free can be intercepted to display debug data, such as printing values of arguments or what not.