Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rtext] DrawTextCodepoint() doesn't check if font is valid, which causes SIGSEGV #4529

Closed
DenizBasgoren opened this issue Nov 21, 2024 · 2 comments

Comments

@DenizBasgoren
Copy link

Issue description

Calling DrawTextCodepoint (and other functions working on fonts, I assume) with an invalid Font structure causes SIGSEGV, presumably due to the following line in the GetGlyphIndex function:

if ((index == 0) && (font.glyphs[0].value != codepoint)) index = fallbackIndex;

Causing this SIGSEGV is easy, as LoadFont doesn't indicate that it failed with an error, and IsFontValid is not used in the examples after loading fonts.

Possible solutions:

  1. DrawTextCodepoint and similar functions call IsFontValid instead of assuming that font is valid.
  2. DrawTextCodepoint and similar functions check font.glyphCount>0 instead of assuming that font is valid.
  3. A boolean flag called isValid is added to the Font structure, indicating that the font is valid, and functions like DrawTextCodepoint check it each time they're called.
  4. LoadFont returns an int which indicates that loading the font failed.
  5. IsFontValid is shown in all examples.

Environment

Linux 6.11

Issue Screenshot

Code Example

int main(void) {
    Font font = LoadFont("font.ttf"); // a non-existent font
    printf("Glyph count = %d\n", font.glyphCount); // 0
    InitWindow(800, 600, "title");
    while (!WindowShouldClose()) {
        BeginDrawing();
            DrawTextCodepoint(
                font,
                'a',
                (Vector2){100,100},
                100,
                GREEN
            );
        EndDrawing();
    }
}
@raysan5 raysan5 changed the title [rtext] DrawTextCodepoint doesn't check if font is valid, which causes SIGSEGV [rtext] DrawTextCodepoint() doesn't check if font is valid, which causes SIGSEGV Nov 22, 2024
@raysan5
Copy link
Owner

raysan5 commented Nov 22, 2024

@DenizBasgoren thanks for reporting, fixed!

@DenizBasgoren
Copy link
Author

A simple check like if (font.glyphCount == 0) return 0; would also work instead of a call to IsFontValid(). I think that wouldn't incur a significant delay. Honestly, a call to IsFontValid() is also not that wasteful imo, as it only does a few if checks. Compared to the rest of GetGlyphIndex() it's no big deal. We have to measure the added delays before drawing the conclusion that it's really wasteful...
In my opinion, what's important here than performance is to prevent a possible SIGSEGV, which I demonstrated that it happens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants