comparison libpurple/imgstore.c @ 22607: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 2f4b10ee752b
comparison
equal deleted inserted replaced
22606:76af23621e19 22607:e09650135f04
32 #include "debug.h" 32 #include "debug.h"
33 #include "imgstore.h" 33 #include "imgstore.h"
34 #include "util.h" 34 #include "util.h"
35 35
36 static GHashTable *imgstore; 36 static GHashTable *imgstore;
37 static int nextid = 0; 37 static unsigned int nextid = 0;
38 38
39 /* 39 /*
40 * NOTE: purple_imgstore_add() creates these without zeroing the memory, so 40 * NOTE: purple_imgstore_add() creates these without zeroing the memory, so
41 * NOTE: make sure to update that function when adding members. 41 * NOTE: make sure to update that function when adding members.
42 */ 42 */
71 int 71 int
72 purple_imgstore_add_with_id(gpointer data, size_t size, const char *filename) 72 purple_imgstore_add_with_id(gpointer data, size_t size, const char *filename)
73 { 73 {
74 PurpleStoredImage *img = purple_imgstore_add(data, size, filename); 74 PurpleStoredImage *img = purple_imgstore_add(data, size, filename);
75 if (img) { 75 if (img) {
76 img->id = ++nextid; 76 /*
77 * Use the next unused id number. We do it in a loop on the
78 * off chance that nextid wraps back around to 0 and the hash
79 * table still contains entries from the first time around.
80 */
81 do {
82 img->id = ++nextid;
83 } while (img->id == 0 || g_hash_table_lookup(imgstore, &(img->id)) != NULL);
77 84
78 g_hash_table_insert(imgstore, &(img->id), img); 85 g_hash_table_insert(imgstore, &(img->id), img);
79 } 86 }
80 87
81 return (img ? img->id : 0); 88 return (img ? img->id : 0);