Rework libpng error handling to catch version mismatch errors as well#134
Draft
Rework libpng error handling to catch version mismatch errors as well#134
Conversation
Before 7e55228, there was an assert where even patch version jump caused the plugin to abort. Which happens pretty regularly, last with 1.6.38 -> 39, and is nothing that should be treated as a critical failure (I wonder what "libpng tutorial" was that taken from, sigh). The above-referenced commit removed this assert in order to unblock users, however it meant that a hypotetical upgrade from 1.6.x to 1.7 would cause the png_create_{read,write}_struct() to print for example libpng warning: Application built with libpng-1.7.0 but running with 1.6.39 directly to the stdout (thus, impossible to see on e.g. Android), and then the plugin immediately does on CORRADE_INTERNAL_ASSERT(png.file). This commit attempts to fix it properly. The png_create_{read,write}_struct() API accepts error/warning callbacks on its own, so supplying them there instead of in png_set_error_fn() allows it to direct the version mismatch error to us. Which is how it should have been done in the first place (and again, I have no idea why the *official* libpng documentation doesn't show that, FFS). Unfortunately, libpng directs the major/minor version mismatch *fatal error* to the warning callback, which is extremely confusing, and there's no way to detect and fixup the case apart from comparing the string itself. Sigh. Co-authored-by: Squareys <squareys@googlemail.com>
…sues. This makes the code no longer use png_jmpbuf() in an attempt to fix the clang-cl error, as I suspected the misbehavior was due to longjmp being possibly called even before setjmp() is called in our code. But, going through libpng sources, it seems that for the version mismatch check the png_create_*_struct() itself calls setjmp(), so there should be no problem, and this whole extra complication is not needed. And, indeed, it has no effect on the behavior -- the clang-cl build still crashes or asserts due to some stack corruption.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Before 7e55228, there was an assert where even patch version jump caused the plugin to abort. Which happens pretty regularly, last with 1.6.38 -> 39, and is nothing that should be treated as a critical failure (I wonder what "libpng tutorial" was that taken from, sigh). The above-referenced commit removed this assert in order to unblock users, however it meant that a hypotetical upgrade from 1.6.x to 1.7 would cause the
png_create_{read,write}_struct()to print for exampledirectly to the stdout (thus, impossible to see on e.g. Android), and then the plugin immediately does on
CORRADE_INTERNAL_ASSERT(png.file). This attempts to fix it properly.The
png_create_{read,write}_struct()API accepts error/warning callbacks on its own, so supplying them there instead of inpng_set_error_fn()allows it to direct the version mismatch error to us. Which is how itshould have been done in the first place (and again, I have no idea why the official libpng documentation doesn't show that, FFS).
Unfortunately, libpng directs the major/minor version mismatch fatal error to the warning callback, which is extremely confusing, and there's no way to detect and fixup the case apart from comparing the
string itself.
So that's what the first commit in this PR does. But then the changes cause a total breakage on clang-cl, and only there, with
longjmp/setjmpcausing the program to jump somewhere totally insane, hitting assertions inDebugdestructor code paths that only ever get hit when!Debugis used (which it isn't). Random ideas:https://en.cppreference.com/w/cpp/utility/program/longjmp says the following. The plugin attempts to have everything constructed before the first
longjmpand destructed after the lastsetjmp, but maybe not correctly?The following patch from @Squareys seems to make the test run again, however we don't yet understand why. I refuse to accept that the assertions, which may construct a
Debuginstance on stack, are what's causing problems -- that'd mean having to redesign the whole debug output printing...setjmp()on its own inside the create function (to catch the version error and returnnullptr), could this be the sole reason for everything catching fire? why is that a problem just for clang-cl?jmpbufbecause at the point wherelongjmpis called from the version mismatch error,png.fileisn't filled in yet and thuspng_jmpbuf(file)can't be called. But it makes no difference to the error on clang-cl.longjmp. The only vaguely relevant bugs are:__builtin_setjmpsaves frame pointer incorrectly for x86_64-w64-windows-gnu (MinGW) target llvm/llvm-project#49486