views:

210

answers:

6

I'm looking for interesting war stories on situations where you had a bug because you missed something important in the documentation of a class you were instantiating or a function/method you were using.

For example, I recently messed up by not reading the documentation for the Java Calendar class and noticing that it counts months from 0... Very nasty surprise that I found the first time I was dealing with a date in December.

Any stories, especially in Java, would be appreciated.

(This is for my PhD research, I'm interested in other people's experiences)

+3  A: 

The java.lang.String replaceAll(String regex, String replacement) method accepts regular expression replace expressions within the replacement parameter. (Backreferences etc).

If you ignore this, it works fine until there's a \ or similar in the replacement string.

I suspect this one even could be used in injection attacks.

Edit: The "War story" in this is that I spent about half a day searching for this bug. Some developer had found that he had to insert quad-escapes in a piece of HTML content that was containing inline javascript regular expressions. Half of them were being eaten by the String.replace function. When I discussed this feature with our security guy he also commented that double layers of escaping are also prone to injection problems.

krosenvold
It's interesting, I've never noticed that though I haven't really used this function. It doesn't seem to be in the documentation, though it could be inferred...
Uri
Its there, but you have to follow the link through to the underlying regex replace.
krosenvold
That's fascinating, thanks!My dissertation focuses on lack of directive awareness. For example, the "note that..." in matcher.replace is a directive, and calling replace without awareness can be bad news (my tool "pushes" this). They should have repeated the directive in replaceAll...
Uri
BTW, Have that happened to you or you're just aware of the potential for the bug?
Uri
@krosenvold: Thank you for this excellent example, I've used it successfully in several venues when presenting my work.
Uri
+1  A: 

There are lots of them. Some of them are really subtle, though.

Most people don't know that java.lang.Object.wait (with or without a timeout) may return without timing out or receiving a notification. The documentation says you should always use it in a loop. How often have you seen it in a loop. :)

Dustin
+1  A: 

I've had to deal with ConcurrentModificationExceptions in production code over collections that came from java.util.Collections.unmodifiableCollection instances.

Someone (I'd assume I) apparently didn't know that that does not produce a copy of a collection, but just thinly wraps one, so the underlying collection may still be modified elsewhere and break other things.

Dustin
I've read the entire JDK and you'd be surprised in how many places there is inconsistent treatment of this real copy vs. direct reference situations. My tool highlights the the caller that they should be aware of something. Glad to know it does mess up people's work in practice.
Uri
A: 

I'm also pretty sure that nearly everyone who's tried to learn the servlet API didn't read this:

Servlets typically run on multithreaded servers, so be aware that a servlet must handle concurrent requests and be careful to synchronize access to shared resources. Shared resources include in-memory data such as instance or class variables and external objects such as files, database connections, and network connections. See the Java Tutorial on Multithreaded Programming for more information on handling multiple threads in a Java program.

Every person's first servlet I've ever seen was written like a fire-and-forget CGI with lots of state on the servlet instance that would just fall apart really badly when two people were using the app at the same time.

Dustin
A: 

I've been caught out by the classic Win32 API CreateFile() function (creates or opens a file or I/O device).

On failure, it returns INVALID_HANDLE_VALUE (defined as (DWORD)-1) and not NULL as you might expect. Other functions that return handles to kernel objects return NULL on failure!

Mitch Wheat
A: 

I've forgotten the details but back in 99 or so I got bitten by a similar off by one bug in a php function that used a length rather than an index, which wasn't clear to me based on the documentation.

These days I unit test so I would likely catch my mistake... back then I was a naive novice so it went to production and resulted in a small data loss.

Jason Watkins