# Trim function in C

I am trying to write an idiomatic trim function in C. How does this look? Should I instead malloc the new string and return it?

void trim(const char *input, char *result)
{
int i, j = 0;
for (i = 0; input[i] != '\0'; i++) {
if (!isspace(input[i])) {
result[j++] = input[i];
}
}
}

-
There's a number of problems with that code, it's vulnerable to buffer overflow attacks and it doesn't do what a typical "trim" function does. Trim removes leading and trailing whitespace. This strips them all out. –  Jeff Mercado Jan 25 '13 at 15:12
Thanks. Can you please elaborate on how to handle buffer overflow attacks? –  jay rulher Jan 25 '13 at 15:44
You should never blindly copy data to some buffer when you don't know how much space is allocated to it, that's just asking for trouble. A simple thing to do would be to add a parameter which takes in the size of the buffer. That way it's all on the caller to tell you how big it really is. Then it's up to you to never attempt to read/write beyond the given length. Of course it's not fool proof, the caller can give you bogus lengths, but that would be a problem on their end, not yours. –  Jeff Mercado Jan 25 '13 at 17:05

As @JeffMercado pointed out, this removes spaces instead of trimming leading and trailing spaces. Assuming you want to keep the current functionality, let's call it remove_spaces.

There's a really subtle bug here:

... isspace(input[i]) ...


isspace takes the value of an unsigned char or EOF. Passing it a char, which is usually signed, will produce undefined behavior. Instead, say:

... isspace((unsigned char) input[i]) ...


Another bug: you don't emit a NUL terminator, meaning the caller would have no way of knowing how long the string is (unless it zeroed out the buffer before calling your function).

Fixing these bugs gives us:

void remove_spaces(const char *input, char *result)
{
int i, j = 0;
for (i = 0; input[i] != '\0'; i++) {
if (!isspace((unsigned char) input[i])) {
result[j++] = input[i];
}
}
result[j] = '\0';
}


@JeffMercado also said this function is vulnerable to buffer overflow. In a sense, this is not true, provided the caller knows to allocate a buffer of at least strlen(input) + 1. But the caller might be lazy and just say char result[100]. Adding an output buffer size parameter will likely guard against such a mistake:

void remove_spaces(const char *input, char *output, size_t output_size);


See if you can implement this. Some things to bear in mind:

• Don't forget about the NUL terminator when checking the output buffer size.

• Don't be like strncpy and omit the NUL terminator when you have to truncate the string, as it can lead to subtle bugs.

• If you use int for i and j and size_t for output_size, you should get compiler warnings about comparison between signed and unsigned. If you don't, turn up your compiler warnings. If you're using GCC from the command line, get in the habit of typing gcc -Wall -W.

-

Have you given thought to more of an in-place approach? Here's a quick example (I took one shortcut by taking strlen, which means an extra string traversal):

void trim(char *input)
{
char *dst = input, *src = input;
char *end;

// Skip whitespace at front...
//
while (isspace((unsigned char)*src))
{
++src;
}

// Trim at end...
//
end = src + strlen(src) - 1;
while (end > src && isspace((unsigned char)*end))
{
*end-- = 0;
}

// Move if needed.
//
if (src != dst)
{
while ((*dst++ = *src++));
}
}

-