@@ -6093,21 +6093,26 @@ SDL_MenuItem *SDL_CreateMenuItemAt(SDL_MenuItem *menu_bar_as_item, size_t index,
60936093
60946094 // Get the last item in the list and insert our new item.
60956095 if (menu -> child_list ) {
6096- SDL_MenuItem * current = menu -> child_list ;
6096+ if (index == 0 ) {
6097+ menu_item -> common .next = menu -> child_list ;
6098+ menu -> child_list -> common .prev = menu_item ;
6099+ menu -> child_list = menu_item ;
6100+ } else {
6101+ SDL_MenuItem * current = menu -> child_list ;
6102+ for (size_t i = 1 ; (i < index ) && current ; ++ i ) {
6103+ current = current -> common .next ;
6104+ }
60976105
6098- for (size_t i = 1 ; (i < index ) && current ; ++ i ) {
6099- current = current -> common .next ;
6100- }
6106+ SDL_assert (current );
61016107
6102- SDL_assert (current );
6108+ if (current -> common .next ) {
6109+ current -> common .next -> common .prev = menu_item ;
6110+ menu_item -> common .next = current -> common .next ;
6111+ }
61036112
6104- if (current -> common .next ) {
6105- current -> common .next -> common .prev = menu_item ;
6106- menu_item -> common .next = current -> common .next ;
6113+ current -> common .next = menu_item ;
6114+ menu_item -> common .prev = current ;
61076115 }
6108-
6109- current -> common .next = menu_item ;
6110- menu_item -> common .prev = current ;
61116116 } else {
61126117 menu -> child_list = menu_item ;
61136118 }
@@ -6241,7 +6246,7 @@ bool SDL_DestroyMenuItem(SDL_MenuItem *menu_item)
62416246
62426247 if (menu_item -> common .prev ) {
62436248 menu_item -> common .prev -> common .next = menu_item -> common .next ;
6244- menu_item -> common .next -> common .next = menu_item -> common .prev ;
6249+ menu_item -> common .next -> common .prev = menu_item -> common .prev ;
62456250
62466251 if (menu_item == menu_item -> common .parent -> menu_common .child_list ) {
62476252 menu_item -> common .parent -> menu_common .child_list = menu_item -> common .next ;
0 commit comments