Skip to content

Commit

Permalink
protect -insertborderwidth/-insertwidth/-selborderwidth, which cannot…
Browse files Browse the repository at this point in the history
… be negative (or "")
  • Loading branch information
jan.nijtmans committed Sep 29, 2024
2 parents b78d24b + b5775ed commit 5ec788b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ writing Tcl scripts containing Tk commands.
- [Slow processing irregular transparencies](https://core.tcl-lang.org/tk/tktview/919066)
- [text's cursor width on 0th column](https://core.tcl-lang.org/tk/tktview/47fbfc)
- [text widget breaks graphemes with combining diacritical marks](https://core.tcl-lang.org/tk/tktview/442208)

9 changes: 9 additions & 0 deletions generic/tkCanvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -2327,6 +2327,15 @@ ConfigureCanvas(
Tcl_IncrRefCount(canvasPtr->yScrollIncrementObj);
}
canvasPtr->inset = borderWidth + highlightWidth;
if (canvasPtr->textInfo.insertBorderWidth < 0) {
canvasPtr->textInfo.insertBorderWidth = 0;
}
if (canvasPtr->textInfo.insertWidth < 0) {
canvasPtr->textInfo.insertWidth = 0;
}
if (canvasPtr->textInfo.selBorderWidth < 0) {
canvasPtr->textInfo.selBorderWidth = 0;
}

gcValues.function = GXcopy;
gcValues.graphics_exposures = False;
Expand Down
27 changes: 26 additions & 1 deletion generic/tkText.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ static const Tk_OptionSpec optionSpecs[] = {
{TK_OPTION_PIXELS, "-selectborderwidth", "selectBorderWidth",
"BorderWidth", DEF_TEXT_SELECT_BD_COLOR,
offsetof(TkText, selBorderWidthObj), offsetof(TkText, selBorderWidth),
TK_OPTION_NULL_OK, DEF_TEXT_SELECT_BD_MONO, 0},
0, DEF_TEXT_SELECT_BD_MONO, 0},
{TK_OPTION_COLOR, "-selectforeground", "selectForeground", "Background",
DEF_TEXT_SELECT_FG_COLOR, TCL_INDEX_NONE, offsetof(TkText, selFgColorPtr),
TK_OPTION_NULL_OK, DEF_TEXT_SELECT_FG_MONO, 0},
Expand Down Expand Up @@ -2227,6 +2227,31 @@ ConfigureText(
textPtr->spacing3Obj = Tcl_NewIntObj(0);
Tcl_IncrRefCount(textPtr->spacing3Obj);
}
if (textPtr->insertBorderWidth < 0) {
textPtr->insertBorderWidth = 0;
if (textPtr->insertBorderWidthObj) {
Tcl_DecrRefCount(textPtr->insertBorderWidthObj);
}
textPtr->insertBorderWidthObj = Tcl_NewIntObj(0);
Tcl_IncrRefCount(textPtr->insertBorderWidthObj);
}
if (textPtr->insertWidth < 0) {
textPtr->insertWidth = 0;
if (textPtr->insertWidthObj) {
Tcl_DecrRefCount(textPtr->insertWidthObj);
}
textPtr->insertWidthObj = Tcl_NewIntObj(0);
Tcl_IncrRefCount(textPtr->insertWidthObj);
}
if (textPtr->selBorderWidth < 0) {
textPtr->selBorderWidth = 0;
if (textPtr->selBorderWidthObj) {
Tcl_DecrRefCount(textPtr->selBorderWidthObj);
}
textPtr->selBorderWidthObj = Tcl_NewIntObj(0);
Tcl_IncrRefCount(textPtr->selBorderWidthObj);
}


/*
* Parse tab stops.
Expand Down
10 changes: 5 additions & 5 deletions generic/tkTextMark.c
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,11 @@ TkTextInsertDisplayProc(
height, 0, TK_RELIEF_FLAT);
}
} else if (textPtr->insertUnfocussed == TK_TEXT_INSERT_NOFOCUS_HOLLOW) {
if (textPtr->insertBorderWidth < 1) {
if (textPtr->insertBorderWidth > 0) {
Tk_Draw3DRectangle(textPtr->tkwin, dst, textPtr->insertBorder,
x - halfWidth, y, charWidth + textPtr->insertWidth,
height, textPtr->insertBorderWidth, TK_RELIEF_RAISED);
} else {
/*
* Hack to work around the fact that a "solid" border always
* paints in black.
Expand All @@ -677,10 +681,6 @@ TkTextInsertDisplayProc(
XDrawRectangle(Tk_Display(textPtr->tkwin), dst, borderPtr->bgGC,
x - halfWidth, y, charWidth + textPtr->insertWidth - 1,
height - 1);
} else {
Tk_Draw3DRectangle(textPtr->tkwin, dst, textPtr->insertBorder,
x - halfWidth, y, charWidth + textPtr->insertWidth,
height, textPtr->insertBorderWidth, TK_RELIEF_RAISED);
}
} else if (textPtr->insertUnfocussed == TK_TEXT_INSERT_NOFOCUS_SOLID) {
Tk_Fill3DRectangle(textPtr->tkwin, dst, textPtr->insertBorder,
Expand Down
6 changes: 3 additions & 3 deletions tests/text.test
Original file line number Diff line number Diff line change
Expand Up @@ -3378,18 +3378,18 @@ test text-14.9 {ConfigureText procedure} -setup {
test text-14.10 {ConfigureText procedure} -setup {
text .t -font {Courier -12} -borderwidth 2 -highlightthickness 2
} -body {
.t configure -selectborderwidth {}
.t configure -selectborderwidth 0
.t tag cget sel -borderwidth
} -cleanup {
destroy .t
} -result {}
} -result 0
test text-14.11 {ConfigureText procedure} -setup {
text .t
} -body {
.t configure -selectborderwidth foo
} -cleanup {
destroy .t
} -returnCodes error -result {expected screen distance or "" but got "foo"}
} -returnCodes error -result {expected screen distance but got "foo"}
test text-14.12 {ConfigureText procedure} -body {
text .t
entry .t.e
Expand Down

0 comments on commit 5ec788b

Please sign in to comment.