wiiu-env / libgui

7 stars 3 forks source link

GuiText manipulating underlying string #3

Open V10lator opened 4 years ago

V10lator commented 4 years ago

Code says more than thousand words:

// CODE:

    debugPrintf("Asked to add \"%s\"", str);

    GuiText *text = new GuiText(str);

    text->setPosition(row * 24, column * 24);
    text->setMaxWidth(tvWidth - (row * 24) - 100, GuiText::SCROLL_HORIZONTAL);

    debugPrintf("Adding \"%s\" at %d/%d", text->toUTF8().c_str(), row * 24, column * 24);

    tvWindow->append(text);

/* OUTPUT:
 * Asked to add "Could not download from"
 * Adding "Could not download fromw" at 0/0
 * Asked to add "http://enter.that.title.key/site/here"
 * Adding "http://enter.that.title.key/site/herezKH" at 0/24
 * Asked to add "Press (A) to enter a new URL"
 * Adding "Press (A) to enter a new URL" at 0/72
 * Asked to add "Press (B) to exit"
 * Adding "Press (B) to exit" at 0/96
 */

So it added "w" at the end of the first string, "zKH" at the end of the second, correctly handled the third and added one unprintable char to the end of the fourth.

V10lator commented 4 years ago

I looked a bit more into it and it seems the string return by text->toUTF8().c_str() is simply missing the null terminator.

V10lator commented 4 years ago

First error found: https://github.com/wiiu-env/libgui/blob/master/source/gui/FreeTypeGX.cpp#L76 - if we look at http://man7.org/linux/man-pages/man3/mbstowcs.3.html we see that n should include the NULL character, so it should be

int32_t bt = mbstowcs(strWChar, strChar, strlen(strChar) + 1);

Also it seems the NULL character is discarded here: https://github.com/wiiu-env/libgui/blob/master/source/gui/FreeTypeGX.cpp#L115 (it stops the loop when strChar[i] is NULL which means it stops before it copies the NULL).

Please note that this was just found by overlooking the sourcecodes real quick. I did not test potential fixes in any way.

//EDIT: https://github.com/wiiu-env/libgui/blob/master/source/gui/FreeTypeGX.cpp#L77 is wrong, too. It should be if (bt < strlen(strChar)) { and you might want to add an extra check for -1 (error) before that.