changeset 22624:e09650135f04

Add a little safety... just in case someone imgstores 13 million or however many images
author Mark Doliner <mark@kingant.net>
date Fri, 04 Apr 2008 01:17:29 +0000
parents 76af23621e19
children ae436d5e42d5
files libpurple/imgstore.c
diffstat 1 files changed, 9 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/imgstore.c	Thu Apr 03 23:51:19 2008 +0000
+++ b/libpurple/imgstore.c	Fri Apr 04 01:17:29 2008 +0000
@@ -34,7 +34,7 @@
 #include "util.h"
 
 static GHashTable *imgstore;
-static int nextid = 0;
+static unsigned int nextid = 0;
 
 /*
  * NOTE: purple_imgstore_add() creates these without zeroing the memory, so
@@ -73,7 +73,14 @@
 {
 	PurpleStoredImage *img = purple_imgstore_add(data, size, filename);
 	if (img) {
-		img->id = ++nextid;
+		/*
+		 * Use the next unused id number.  We do it in a loop on the
+		 * off chance that nextid wraps back around to 0 and the hash
+		 * table still contains entries from the first time around.
+		 */
+		do {
+			img->id = ++nextid;
+		} while (img->id == 0 || g_hash_table_lookup(imgstore, &(img->id)) != NULL);
 
 		g_hash_table_insert(imgstore, &(img->id), img);
 	}