Revision: 15056
          http://sourceforge.net/p/skim-app/code/15056
Author:   hofman
Date:     2025-03-30 14:39:44 +0000 (Sun, 30 Mar 2025)
Log Message:
-----------
Fix some validation checks

Modified Paths:
--------------
    trunk/SKColorSwatch.m

Modified: trunk/SKColorSwatch.m
===================================================================
--- trunk/SKColorSwatch.m       2025-03-30 09:31:38 UTC (rev 15055)
+++ trunk/SKColorSwatch.m       2025-03-30 14:39:44 UTC (rev 15056)
@@ -516,10 +516,12 @@
 
 #pragma mark Modification
 
+#define VALID_INDEX(i, op) (i >= 0 && i op (NSInteger)[itemViews count])
+
 - (void)selectColorAtIndex:(NSInteger)idx {
     if (idx == -1) {
         [self deactivate];
-    } else if ([self selects] && idx != selectedIndex && [self isEnabled] && 
[[self window] isMainWindow]) {
+    } else if ([self selects] && VALID_INDEX(idx, <) && idx != selectedIndex 
&& [self isEnabled] && [[self window] isMainWindow]) {
         NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
         NSColorPanel *colorPanel = [NSColorPanel sharedColorPanel];
         if (selectedIndex != -1) {
@@ -548,8 +550,7 @@
         NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
         [nc removeObserver:self name:NSColorPanelColorDidChangeNotification 
object:[NSColorPanel sharedColorPanel]];
         [nc removeObserver:self 
name:SKColorSwatchOrWellWillActivateNotification object:nil];
-        if (selectedIndex != -1)
-            [[itemViews objectAtIndex:selectedIndex] setSelected:NO];
+        [[itemViews objectAtIndex:selectedIndex] setSelected:NO];
         [self willChangeValueForKey:SELECTEDCOLORINDEX_KEY];
         selectedIndex = -1;
         [self didChangeValueForKey:SELECTEDCOLORINDEX_KEY];
@@ -589,10 +590,8 @@
     }
 }
 
-#define VALID_INDEX(i) (i >= 0 && i <= (NSInteger)[itemViews count])
-
 - (void)_setColor:(NSColor *)color atIndex:(NSInteger)i {
-    if (VALID_INDEX(i)) {
+    if (VALID_INDEX(i, <)) {
         [self willChangeColors];
         SKColorSwatchItemView *itemView = [itemViews objectAtIndex:i];
         [itemView setColor:color];
@@ -603,7 +602,7 @@
 
 - (void)setColor:(NSColor *)color atIndex:(NSInteger)i {
     [self _setColor:color atIndex:i];
-    if (VALID_INDEX(i) && selectedIndex == i) {
+    if (VALID_INDEX(i, <=) && selectedIndex == i) {
         NSColorPanel *colorPanel = [NSColorPanel sharedColorPanel];
         NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
         [nc removeObserver:self name:NSColorPanelColorDidChangeNotification 
object:colorPanel];
@@ -613,7 +612,7 @@
 }
 
 - (void)insertColor:(NSColor *)color atIndex:(NSInteger)i {
-    if (VALID_INDEX(i)) {
+    if (VALID_INDEX(i, <)) {
         [self willChangeColors];
         bezelWidth = [self contentWidth];
         SKColorSwatchItemView *itemView = [[SKColorSwatchItemView alloc] 
initWithFrame:[self frameForCollapsedItemViewAtIndex:i]];
@@ -644,7 +643,7 @@
 }
 
 - (void)removeColorAtIndex:(NSInteger)i {
-    if (VALID_INDEX(i)) {
+    if (VALID_INDEX(i, <) && [itemViews count] > 1) {
         if (selectedIndex == i)
             [self deactivate];
         [self willChangeColors];
@@ -675,7 +674,7 @@
 }
 
 - (void)moveColorAtIndex:(NSInteger)from toIndex:(NSInteger)to {
-    if (VALID_INDEX(from) && VALID_INDEX(from) && from != to) {
+    if (VALID_INDEX(from, <) && VALID_INDEX(to, <) && from != to) {
         [self willChangeColors];
         SKColorSwatchItemView *itemView = [itemViews objectAtIndex:from];
         [itemViews removeObjectAtIndex:from];

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Skim-app-commit mailing list
Skim-app-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to