TStringGrid copy/paste cell containing ampersand enters infinite loop
Original Reporter info from Mantis: aphid
-
Reporter name: eric maw
Original Reporter info from Mantis: aphid
- Reporter name: eric maw
Description:
in TCustomStringGrid.SelectionSetHTML, local function ReplaceEntities:
if a cell in a StringGrid contains either:
- an ampersand, followed later by a semicolon, where the intervening text does not represent a valid HTML entity name.
- an ampersand, followed later by a character which is converted to an HTML entity, or explicit text representing a valid entity
if the content of such a cell is copied (without invoking an editor instance, so that it is copied to the clipboard as HTML rather than plain text), then pasted into a cell the process does not return.
apparent cause of problem:
-within the processing loop of ReplaceEntities, the scan origin is set to the address of cSt[1] at the start of each loop.
in case 1) a potential entity is found, but is invalid, so that ResolveHTMLEntityReference returns false and the string is not modified. The loop is then restarted at the beginning of the string and the pseudo-entity refound infinitely
in case 2), "&" is correctly replaced by "&", but since the origin is set to the start of the modified string, the inserted "&" is found and initiates a scan which terminates with the end of the following valid entity, but the accumulated substring represents an invalid name and the problem reduces to case 1.
Steps to reproduce:
-add a stringgrid component to a form, set goEditing to true, and run the form
-enter either the text "x&x;x" or the text "x&x&x" into a cell
-select the cell (without activating an editor instance) and ctl-C copy
-paste the resultant clipboard contents into a grid cell - the process does not return
Additional information:
I have implemented a simple working modification for my own purposes, but do not do enough coding to feel comfortable with the mix of ansistring, widestring, rawstringbuffer, and (especially) pchar involved, and so not sure that my solution is free of side-effects or is particularly efficient.
Mantis conversion info:
- Mantis ID: 37258
- OS: Windows 10 home
- OS Build: 1909
- Platform: pentium E5700
- Version: 2.0.4
- Fixed in version: 2.0.10
- Fixed in revision: r63457 (#02ae498e)
- Target version: 2.0.10