diff options
author | Graeme Geldenhuys <graeme@mastermaths.co.za> | 2010-03-01 09:58:20 +0200 |
---|---|---|
committer | Graeme Geldenhuys <graeme@mastermaths.co.za> | 2010-03-01 09:58:20 +0200 |
commit | e5e651778ea8805e03c69adacbca099e9b5ef8b7 (patch) | |
tree | c58037221d403f88e7feecffc86c43e265b66d2b /src/corelib | |
parent | 568d232144d05f8d2de416d28d426a8bb631fea3 (diff) | |
download | fpGUI-e5e651778ea8805e03c69adacbca099e9b5ef8b7.tar.xz |
X11: correctly handle buffer overflow errors in Xutf8LookupString API call.
We never handled buffer overflow correctly before.
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/x11/fpg_x11.pas | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/src/corelib/x11/fpg_x11.pas b/src/corelib/x11/fpg_x11.pas index 7e6d28ea..8d0fcb21 100644 --- a/src/corelib/x11/fpg_x11.pas +++ b/src/corelib/x11/fpg_x11.pas @@ -230,7 +230,7 @@ type TfpgX11Application = class(TfpgApplicationBase) private - FComposeBuffer: String[32]; + FComposeBuffer: TfpgString; FComposeStatus: TStatus; FEventFilter: TX11EventFilter; function ConvertShiftState(AState: Cardinal): TShiftState; @@ -669,10 +669,16 @@ function TfpgX11Application.StartComposing(const Event: TXEvent): TKeySym; var l: integer; begin + SetLength(FComposeBuffer, 20); // buffer set to some default size // Xutf8LookupString returns the size of FComposeBuffer in bytes. l := Xutf8LookupString(InputContext, @Event.xkey, @FComposeBuffer[1], - SizeOf(FComposeBuffer) - 1, @Result, @FComposeStatus); + Length(FComposeBuffer), @Result, @FComposeStatus); SetLength(FComposeBuffer, l); + // if overflow occured, then previous SetLength() would have fixed the buffer + // size, so run Xutf8LookupString again to read correct value. + if FComposeStatus = XBufferOverflow then + Xutf8LookupString(InputContext, @Event.xkey, @FComposeBuffer[1], + Length(FComposeBuffer), @Result, @FComposeStatus); end; function TfpgX11Application.DoGetFontFaceList: TStringList; @@ -849,9 +855,9 @@ var procedure PrintKeyEvent(const event: TXEvent); var keysym: TKeySym; - compose_status: TXComposeStatus; - length: integer; - s: string[10]; + icstatus: TStatus; + l: integer; + s: string; begin case event._type of X.KeyPress: @@ -867,10 +873,12 @@ var writeln('not a key event '); end; end; - length := Xutf8LookupString(InputContext, @event.xkey, @s[1], 9, @keysym, @compose_status); - SetLength(s, length); - if((length > 0) and (length <=9)) then - writeln('result of xlookupstring [' + s + ']'); + SetLength(s, 20); + l := Xutf8LookupString(InputContext, @event.xkey, @s[1], Length(s), @keysym, @icstatus); + SetLength(s, l); + if icstatus = XBufferOverflow then + Xutf8LookupString(InputContext, @event.xkey, @s[1], Length(s), @keysym, @icstatus); + writeln('result of xlookupstring [' + s + ']'); writeln(Format('*** keysym [%s] ', [XKeysymToString(keysym)])); end; |