Add 88 new defect entries to HIGH and MEDIUM tables:
HIGH: mysql-0001/0002, mariadb-0001, redis-0001/0002, valkey-0001/0002, openvpn-0001,
vlc-0001, prometheus-0001, otel-collector-0001, cockroachdb-0001..0004,
tidb-0001..0008, kubernetes-0001/0002, go-0001, kotlin-0002, scala-0001,
allegro5-0001, sdl2-0001, grafana-0001, clickhouse-0001, duckdb-0001,
mongodb-0001, envoy-0001, istio-0001, cilium-0001, linkerd2-0001,
linux-0001/0002/0003, tor-0002/0003, curl-0001, julia-0001, lua-0001,
perl5-0001, nats-0001, spring-0003/0004, tomcat-0001, onos-0002, odl-0002
MEDIUM: helm-0001, mariadb-0002, openssl-0001/0002, memcached-0001,
cassandra-0001..0004, flink-0001, storm-0001/0002, zookeeper-0001..0003,
pip-0001, gradle-0001, nginx-0001, haproxy-0001, caddy-0001, varnish-0001,
ffmpeg-0001, gstreamer-0001, raylib-0001, love2d-0001, php-0001/0002,
r-source-0001, cpython-0002, ruby-0001, rabbitmq-0003/0004, activemq-0001,
ovs-0001, onos-0003, odl-0002, jetty-0001
PDF: 976K
2.1 KiB
raylib-0001 — GetGlyphIndex O(n) linear scan per character per DrawText call
Status
PATCHED
Severity
HIGH
Target
raylib src/rtext.c
CWE
CWE-407: Algorithmic Complexity — Insufficient Complexity Reduction Before Algorithmic Intensive Operation
Description
GetGlyphIndex(Font font, int codepoint) performs an O(n) linear scan of
font.glyphs[] for every character in every text draw call (DrawText, DrawTextEx,
MeasureText, MeasureTextEx, DrawTextCodepoint, etc.). For a font with N glyphs,
rendering a string of L characters costs O(L * N) per frame.
In the SUPPORT_UNORDERED_CHARSET branch (enabled by default via #define), every
call walks the full glyphs array until it finds the matching codepoint or exhausts the
array.
// rtext.c:1453
int GetGlyphIndex(Font font, int codepoint)
{
int index = 0;
...
for (int i = 0; i < font.glyphCount; i++) // O(n) every call
{
if (font.glyphs[i].value == 63) fallbackIndex = i;
if (font.glyphs[i].value == codepoint) { index = i; break; }
}
...
}
Call Sites (partial)
DrawText→DrawTextEx→ inner loop callsGetGlyphIndexper codepointMeasureTextEx→ inner loop callsGetGlyphIndexper codepointDrawTextCodepoints→ inner loopGetGlyphInfo→GetGlyphIndexGetGlyphAtlasRec→GetGlyphIndex
Fix
Build a codepoint → index hash map (or sorted array + binary search) once at font
load time. GetGlyphIndex then becomes O(1) average (hash map) or O(log n) (binary
search on sorted glyphs).
Patch: sort glyphs by codepoint value at load time and use binary search. This keeps memory layout unchanged and requires no new heap allocation.
Complexity
- Before: O(L * N) per DrawText call where L = string length, N = font.glyphCount
- After: O(L * log N) with binary search; O(L) with hash map
Benchmark
See defects/raylib/unit/RaylibGlyphIndexTest.java — at N=2048 glyphs:
- slow() (linear scan): ~2048 comparisons per lookup
- fast() (binary search): ~11 comparisons per lookup
- Ratio: ~186x
Files
defects/raylib/patch/raylib-0001.patchdefects/raylib/unit/RaylibGlyphIndexTest.java