Jump to content

TextEdit gwinSetText memory corruption issue


neon1

Recommended Posts

Hello,

I've been troubleshooting a memory corruption issue triggered by keyboard events in a TextEdit widget after gwinSetText() has been used on it. It is probably the same issue described a bit cryptically here: https://community.ugfx.io/topic/415-keyboard-and-textedit-problem/. Of course, as a µGFX newbie I may be simply misunderstanding something, so in that case feel free to tell me off.

Reproduction:

  1. Create a TextEdit widget.
  2. Type some text into it using the (virtual) keyboard.
  3. Clear the widget's text by calling gwinSetText(ghTextedit, NULL, FALSE), without changing the focus.
  4. Do not tap/touch the TextEdit widget at this point.
  5. Type another character => memory corruption (likely causing a crash/hang/whatever).

Analysis:

  • Looking at gwin_textedit.c, the cause seems to be that the TextEdit widget does not learn of the fact that its text buffer has been replaced, and thus does not update/reset its cursorPos. cursorPos may then point at a position beyond the new buffer, causing memory corruption the next time a character is inserted or deleted.
  • If the user taps the TextEdit widget after the text has been set and before typing again, the cursorPos is reset to a reasonable value and the issue does not appear.

Workaround:

  • Do not use gwinSetText() on TextEdit, but recreate the widget instead.

Fix:

  • A clean fix with proper cursorPos resetting would probably require TextEdit to be notified of text changes.
  • A sort-of fix by checking cursorPos before using it is attached.

Thanks for creating and maintaining µGFX! It's the best GUI library of its kind that I've come across, and the out-of-the-box virtual keyboard is a killer feature ?

– Manuel

patch-textedit-cursorpos.diff

Link to comment
Share on other sites

42 minutes ago, wctltya said:

@neon1 Have you tried instead of passing NULL pointer here gwinSetText(ghTextedit, NULL, FALSE), to pass a pointer to a char buff, which first element is 0, or just 

gwinSetText(ghTextedit, "", FALSE) ?

Yes, but as can be seen on line 525 of gwin_widget.c (µGFX v2.8), both are treated the same:

if (!text || !*text)
	gw->text = "";

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...