Is libpng crap ? and php ?

As I told before, I’ve been working for a few days on a way to display japanese characters on a web page when you don’t have japanese fonts locally. That involved taking japanese characters from a font file, and turning it into a png image. So I just wrote a small and simple program using libpng and freetype. Which works quite well. But that’s not the point.

During this “development”, I lost 2 or 3 days trying to understand what was wrong with my code that made it work properly from a shell command and fail halfway when wrapped in a php script. I still don’t know what’s wrong with php, but it works well with a perl cgi wrapper, and it still fails if I raise the php memory limit. Interestingly, other libpng tools (such as pngtopnm) fail in the same way. It wouldn’t surprise me if it was libpng’s fault.

The first thing i did to understand why it’d fail, was to run the program under valgrind. And what I saw was not encouraging. A lot of “Conditional jump or move depends on uninitialised value(s)”. I tried different combinations of code, using png_write_image or png_write_row, with large or small buffers…

It turns out libpng can’t write a png file with a width smaller than 148 pixels without those “Conditional jump or move depends on uninitialised value(s)”, which happen, as the stack trace shows, in the zlib, called by png_write_row, but i doubt it to be a zlib issue. You would think it would be a buffer size issue, but if you use a larger buffer for each line you call png_write_row for, you still get the errors.

If anyone has a clue, please leave a comment.

Update: Thanks Mark for your comment, though I don’t get how it can be safe to have such accesses to uninitialized memory. Deuce, the php issue can’t be a timeout issue, the program runs in fractions of a second.

2006-02-19 14:21:32+0900

miscellaneous, p.d.o

Both comments and pings are currently closed.

3 Responses to “Is libpng crap ? and php ?”

  1. Technicalities » zlib generating valgrind warnings Says:

    […] Mike assumes that zlib is valgrind clean. In actual fact there are a number of cases where loop unrolling in the zlib library causes the mentioned “Conditional jump or move depends on uninitialised value(s)” warnings. These are safe since the results of the comparison are subsequently ignored but obviously valgrind doesn’t know that. This is discussed briefly in the zlib FAQ. Recent valgrind packages should have rules suppress the spurious warnings from zlib, though I don’t know if the rules manage to cover all the relevant cases or not and obviously it is still possible to pass uninitialised data into zlib.   […]

  2. Deuce Says:

    I’ve not used the libraries you’re talking about but whenever code works from the command line and not from the apache served version of php I immediately go to timeouts. In command line php does not have a script timeout by default while from the web there is a 30 second timeout. Just another idea to drop. Regardless of buffer sizes, the time it takes might be hitting you.

  3. Mark Brown Says:

    The accesses are safe because zlib is working from a buffer it itself allocated (so it knows that the pointer is valid) and bounds checking done after the loop exits causes the results of the calculations done with the uninitialised memory to be discarded.