SDL+OpenGL bug of some sort

I have a little SDL+OpenGL thing I’ve written and my problem is the
following. I have a screenshot function to dump the current contents of
the OpenGL window to file. I use glReadPixels() with a pixel format of
GL_RGB to get the data. If I resize the OpenGL window with the window
manager subsequent calls to the screenshot function always give some sort
of a distorted image. The runs fine with efence and no overruns seem to
be present. The data in the output file at first looks like it is RGBA
data but I’ve seen that it’s not quite that simple. I’ve tried this on
various platforms and with various window managers. The bug appears on
FreeBSD+Mesa/Solaris 8 with Creator 3d/Linux+Nvidia drivers/Linux+Mesa.
An interesting thing is that this problem doesn’t occur if I ask for the
data in GL_RGBA format and repack it into RGB myself before writing out.
That doesn’t sound like a good idea because the other approach should work
just fine, there’s a bug somewhere.

The source code is at:

util.c has the screenshot function at the end.

An example of the output distorted output:

– Arnar