Fixed a *Big* Bug In Routing
- From:
- Zed A. Shaw
- Date:
- 2011-08-02 @ 01:56
I was doing some performance tuning and removing unnecessary calls to
calloc and h_calloc, when I hit this bizarre bug in a totally unrelated
part of the code. Experience has taught me when you hit weird bugs in
unrelated parts of C code, it's because of memory. After spending 2
days debugging this, it came down to some *STUPID* code I wrote for the
routing and pattern matching.
The gory details of the patch are here:
https://github.com/zedshaw/mongrel2/commit/ec7a1a64808b3aed1d99a850f4cd37aaffe82dae
But the short story is that, there were three *different* bugs in the
damn routing code:
1. I was subtracting 2 unsigned size_t types, and then using that
directly as the pattern length. This works when the pattern is smaller
than what you're testing it against, but if the pattern was bigger, then
you'd get a < 0 number, which when converted to unsigned turns into a
huge number. Previously using calloc meant it wasn't an issue because
the pattern code would stop at the first \0, but once I removed it the
pattern code was going to town on the ram. This fix is now at
routing.c:174.
2. I don't even know why this worked, but basically the suffix routes,
for things like host patterns like (.*)zedshaw.com weren't even remotely
correct. I was using the same pattern matching code for those as for
the prefix ones (like used with URLs). But, the suffix ones have the
pattern in front and match in the reverse, so they are *totally*
different. It's only been working because people haven't been doing
very complex host matching, usually just (.*)hostname.com which always
works. This is now fixed at routing.c:195 in match_route_pattern.
3. The test suite for suffix matching didn't even come close to testing
the various edge cases. The tests were fine for the broken code, but
once I fixed the code it was clear there were all sorts of additional
tests needed. This is now fixed.
I'm going to review this code one more time tonight, but please pull the
latest and try it out, then report back to me if you get any problems.
I also haven't done this on OSX yet.
Especially play with this if you were getting odd results on host
matching.
--
Zed A. Shaw
http://zedshaw.com/