Fix panic in Bitmap::decode on zero-width/height EBDT glyphs - #140
Open
youdie006 wants to merge 1 commit into
Open
Fix panic in Bitmap::decode on zero-width/height EBDT glyphs#140youdie006 wants to merge 1 commit into
youdie006 wants to merge 1 commit into
Conversation
Bitmap::decode panicked with 'chunk size must be non-zero' on bitmap glyphs whose width (or height) is 0. Such glyphs are valid font data - blank glyphs in embedded bitmap strikes (EBLC/EBDT) of CJK fonts such as SimSun. For w==0 the row stride ((w*bits)+7)/8 evaluates to 0 and slice::chunks(0) panics; decoded_size() is also 0 so the earlier target.len() < size check does not filter these out. FreeType returns an empty bitmap for these glyphs rather than failing. Guard decode for zero-sized bitmaps and return an empty (successful) decode before the row loop. Returning true matches the crate's own bitmap::resize, which already treats zero dimensions as a successful empty result, and FreeType's empty-bitmap behavior. Fixes dfrg#139
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.
Summary
Fixes #139.
Bitmap::decodepanics withchunk size must be non-zeroon bitmap glyphs whose width (or height) is 0. Such glyphs are valid font data -- blank glyphs in embedded bitmap strikes (EBLC/EBDT) of CJK fonts such as SimSun (simsun.ttc). Any caller usingSource::Bitmap(..)/Scaler::scale_bitmap*on such a font hits the panic while rendering ordinary text.Root cause
For
w == 0, the row stride((w * bits) + 7) / 8evaluates to 0, andslice::chunks(0)panics by contract. This happens in all threeBitmapFormat::Alpha(1|2|4)branches (src/strike.rs).decoded_size()is also 0, so the earliertarget.len() < sizecheck does not filter these out. FreeType returns an empty bitmap for these glyphs rather than failing, so native apps render such fonts without issues.Fix
Guard
decodefor zero-sized bitmaps and return an empty (successful) decode before reaching the row loop:Semantics:
true(empty decode) vsfalse(fallback)The issue suggested two options. This PR returns
true(decode succeeds with an empty image), for two reasons:bitmap::resizealready does exactlyif target_width == 0 || target_height == 0 { return true; }(src/scale/bitmap/mod.rs), treating zero dimensions as a successful empty result -- sodecodereturningtruematches the existing convention.The alternative is
return false, which makes the caller fall back to the nextSource(e.g.Outline). Happy to switch tofalseif you'd prefer blank bitmap glyphs to fall through to outline rendering -- let me know which semantics you want.Test
Added an in-crate regression test (
decode_zero_width_bitmap_does_not_panic) coveringAlpha(1|2|4)zero-width bitmaps plus a zero-height case. Verified red/green:src/strike.rs:439: chunk size must be non-zero.cargo testgreen;cargo fmt --checkclean; no newcargo clippywarnings.This contribution was prepared with AI assistance and reviewed by me before submission.