grimfang4 / SDL_FontCache

A generic font caching C library with loading and rendering support for SDL.
MIT License
305 stars 66 forks source link

FC_GetBounds always returns zero width #32

Closed D0ntPanic closed 5 years ago

D0ntPanic commented 5 years ago

The FC_GetBounds function formats the passed text into fc_buffer, then passes that buffer to the FC_GetWidth and FC_GetHeight functions. These in turn perform another formatting of text into the same fc_buffer destination.

Effectively, it is performing the following action inside FC_GetWidth and FC_GetHeight:

snprintf(fc_buffer, "%s", fc_buffer)

This is undefined behavior. On many platforms, it works fine. However, on Ubuntu 18.04 with GCC version 7.3.0, this causes fc_buffer to receive the empty string no matter what the input is. Since the string is now empty, the resulting width of the string is always zero.

The fix would be to use two different buffers, or make a function that computes the width and height on the string directly without formatting it.

Worked around the problem by using FC_GetWidth and FC_GetHeight directly.

grimfang4 commented 5 years ago

Thanks for the catch. I used the first option, but the second is a better solution to avoid dynamic allocations (create function that assumes non-format strings).