Skip to content

Commit

Permalink
Fix [b189559e4]: Crash with menus and -postcommand option
Browse files Browse the repository at this point in the history
  • Loading branch information
jan.nijtmans committed Oct 12, 2023
1 parent d37c72a commit c503b4d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
22 changes: 11 additions & 11 deletions generic/tkMenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ MenuWidgetObjCmd(
if (menuPtr->active == index) {
goto done;
}
if ((index != TCL_INDEX_NONE) && ((menuPtr->entries[index]->type==SEPARATOR_ENTRY)
if ((index >= 0) && ((menuPtr->entries[index]->type==SEPARATOR_ENTRY)
|| (menuPtr->entries[index]->state == ENTRY_DISABLED))) {
index = TCL_INDEX_NONE;
}
Expand Down Expand Up @@ -826,7 +826,7 @@ MenuWidgetObjCmd(
case MENU_ID: {
Tcl_Size index;
const char *idStr;
Tcl_HashEntry *entryPtr;
Tcl_HashEntry *entryPtr;

if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "index");
Expand All @@ -835,14 +835,14 @@ MenuWidgetObjCmd(
if (GetMenuIndex(interp, menuPtr, objv[2], 0, &index) != TCL_OK) {
goto error;
}
if (index == TCL_INDEX_NONE) {
if (index < 0) {
goto done;
}
entryPtr = menuPtr->entries[index]->entryPtr;
if (entryPtr) {
idStr = (const char *)Tcl_GetHashKey(&menuPtr->items, entryPtr);
Tcl_SetObjResult(interp, Tcl_NewStringObj(idStr, TCL_INDEX_NONE));
}
entryPtr = menuPtr->entries[index]->entryPtr;
if (entryPtr) {
idStr = (const char *)Tcl_GetHashKey(&menuPtr->items, entryPtr);
Tcl_SetObjResult(interp, Tcl_NewStringObj(idStr, TCL_INDEX_NONE));
}
break;
}
case MENU_INDEX: {
Expand Down Expand Up @@ -2157,7 +2157,7 @@ GetMenuIndex(

if (TkGetIntForIndex(objPtr, menuPtr->numEntries - 1, lastOK, indexPtr) == TCL_OK) {
/* TCL_INDEX_NONE is only accepted if it does not result from a negative number */
if (*indexPtr != TCL_INDEX_NONE || Tcl_GetString(objPtr)[0] != '-') {
if (*indexPtr >= 0 || Tcl_GetString(objPtr)[0] != '-') {
if (*indexPtr >= menuPtr->numEntries) {
*indexPtr = menuPtr->numEntries - ((lastOK) ? 0 : 1);
}
Expand Down Expand Up @@ -2679,7 +2679,7 @@ TkActivateMenuEntry(
TkMenuEntry *mePtr;
int result = TCL_OK;

if (menuPtr->active != TCL_INDEX_NONE) {
if (menuPtr->active >= 0) {
mePtr = menuPtr->entries[menuPtr->active];

/*
Expand All @@ -2693,7 +2693,7 @@ TkActivateMenuEntry(
TkEventuallyRedrawMenu(menuPtr, menuPtr->entries[menuPtr->active]);
}
menuPtr->active = index;
if (index != TCL_INDEX_NONE) {
if (index >= 0) {
mePtr = menuPtr->entries[index];
mePtr->state = ENTRY_ACTIVE;
TkEventuallyRedrawMenu(menuPtr, mePtr);
Expand Down
10 changes: 5 additions & 5 deletions macosx/tkMacOSXFont.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ startOfClusterObjCmd(
Tcl_SetErrorCode(interp, "TK", "VALUE", "INDEX", NULL);
return TCL_ERROR;
}
if (index != TCL_INDEX_NONE) {
if (index >= 0) {
if ((size_t)index >= [S length]) {
index = (Tcl_Size)[S length];
} else {
Expand Down Expand Up @@ -508,8 +508,8 @@ endOfClusterObjCmd(
return TCL_ERROR;
}
if ((size_t)index + 1 <= [S length]) {
if (index == TCL_INDEX_NONE) {
index = 0;
if (index < 0) {
index = 0;
} else {
NSRange range = [S rangeOfComposedCharacterSequenceAtIndex:index];
index = range.location + range.length;
Expand Down Expand Up @@ -1011,7 +1011,7 @@ TkpMeasureCharsInContext(
double width;
int length, fit;

if (rangeStart == TCL_INDEX_NONE || rangeStart < 0 || rangeLength <= 0 ||
if (rangeStart < 0 || rangeLength <= 0 ||
rangeStart + rangeLength > numBytes ||
(maxLength == 0 && !(flags & TK_AT_LEAST_ONE))) {
*lengthPtr = 0;
Expand Down Expand Up @@ -1294,7 +1294,7 @@ TkpDrawAngledCharsInContext(
CGAffineTransform t;
CGFloat width, height, textX = (CGFloat) x, textY = (CGFloat) y;

if (rangeStart == TCL_INDEX_NONE || rangeLength + 1 <= 1 ||
if (rangeStart < 0 || rangeLength <= 0 ||
rangeStart + rangeLength > numBytes ||
!TkMacOSXSetupDrawingContext(drawable, gc, &drawingContext)) {
return;
Expand Down
2 changes: 1 addition & 1 deletion macosx/tkMacOSXMenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1592,7 +1592,7 @@ GenerateMenuSelectEvent(
if (menuPtr) {
Tcl_Size index = [menu tkIndexOfItem:menuItem];

if (index == TCL_INDEX_NONE || index >= menuPtr->numEntries ||
if (index < 0 || index >= menuPtr->numEntries ||
(menuPtr->entries[index])->state == ENTRY_DISABLED) {
TkActivateMenuEntry(menuPtr, TCL_INDEX_NONE);
} else {
Expand Down

0 comments on commit c503b4d

Please sign in to comment.