fix possible crash on user deletion
[srvx.git] / src / gline.c
index 0b2ed9bb36f25d4ea6e1c5ef137f0aee7a8beb6a..bb169ab1cadca42875d0f6c5ddf54186c0832cb0 100644 (file)
@@ -52,7 +52,7 @@ static int
 gline_comparator(const void *a, const void *b)
 {
     const struct gline *ga=a, *gb=b;
-    return ga->expires - gb->expires;
+    return ga->lifetime - gb->lifetime;
 }
 
 static void
@@ -72,10 +72,9 @@ free_gline(struct gline *ent)
 }
 
 static int
-gline_for_p(UNUSED_ARG(void *key), void *data, void *extra)
+gline_equal_p(UNUSED_ARG(void *key), void *data, void *extra)
 {
-    struct gline *ge = data;
-    return !irccasecmp(ge->target, extra);
+    return data == extra;
 }
 
 static void
@@ -125,21 +124,19 @@ gline_add(const char *issuer, const char *target, unsigned long duration, const
         lifetime = expires;
     ent = dict_find(gline_dict, target, NULL);
     if (ent) {
-        heap_remove_pred(gline_heap, gline_for_p, (char*)target);
-        if (ent->expires != expires)
-            ent->expires = expires;
+        heap_remove_pred(gline_heap, gline_equal_p, ent);
+        if (ent->issued > lastmod)
+            ent->issued = lastmod;
+        if (ent->lastmod < lastmod) {
+            ent->lastmod = lastmod;
+           ent->expires = expires;
+           if (strcmp(ent->reason, reason)) {
+               free(ent->reason);
+               ent->reason = strdup(reason);
+           }
+       }
         if (ent->lifetime < lifetime)
             ent->lifetime = lifetime;
-        if (ent->lastmod < lastmod)
-            ent->lastmod = lastmod;
-        if (strcmp(ent->issuer, issuer)) {
-            free(ent->issuer);
-            ent->issuer = strdup(issuer);
-        }
-        if (strcmp(ent->reason, reason)) {
-            free(ent->reason);
-            ent->reason = strdup(reason);
-        }
     } else {
         ent = malloc(sizeof(*ent));
         ent->issued = issued;