fixed potential segfaults and inf loops in shorten_text_up_to_width

This commit is contained in:
Andrei "Akari" Alexeyev 2017-08-28 16:00:28 +03:00
parent c593dd7885
commit 1710ca7f9c
No known key found for this signature in database
GPG key ID: 048C3D2A5648B785

View file

@ -227,10 +227,16 @@ int charwidth(char c, TTF_Font *font) {
void shorten_text_up_to_width(char *s, float width, TTF_Font *font) {
while(stringwidth(s, font) > width) {
s[strlen(s) - 1] = 0;
int l = strlen(s);
for(int i = 0; i < 3; ++i) {
if(l <= 1) {
return;
}
--l;
s[l] = 0;
for(int i = 0; i < min(3, l); ++i) {
s[l - i - 1] = '.';
}
}