joyent/node — pull request #2659 by rvermillion (2012-02-01 03:14:47+00:00)
Title: Added a check for zero-length string to fix issue #2657

The following code simply checks for zero-length strings in Buffer::Base64Write and handles them gracefully.

Without this patch, there is an intermittent failure when writing zero-length strings in Base64. On occasion, the buffer will be pulled from the buffer pool because the required length is zero and the remaining bytes in the pool is also zero. But then when the empty string is actually written to the provided buffer, it throws a TypeError because the offset is equal to the buffer length. (See code for details).

If an attempt to write a zero-length string is made and the offset is less than or equal to the buffer length, Buffer::Base64Write will now behave nicely and return a 0 as the count of characters written.

This bug was biting me during use of node-hbase which uses Buffer to decode base64 strings sent from the HBase REST server.

Hope this is helpful. I'm new to github (and git) so apologies if I've done things incorrectly.