Valgrindで見つかったメモリー周辺の問題点を修正
[YACASL2.git] / src / label.c
index 2c4214e..a8baa1d 100644 (file)
 static int labelcnt = 0;                /* ラベル数 */
 static LABELTAB *labels[LABELTABSIZE];  /* ラベル表 */
 
-#ifndef UNITTEST
-static unsigned labelhash(const char *prog, const char *label);
-
-static int compare_adr(const void *a, const void *b);
-#endif
-
 /**
  * プログラム名とラベルに対応するハッシュ値を返す
  */
 unsigned labelhash(const char *prog, const char *label)
 {
     HKEY *keys[2];
-    int i = 0;
+    int i = 0, j;
+    unsigned h;
 
     if(prog != NULL) {
-        keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key[]");
+        keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key");
         keys[i]->type = CHARS;
-        keys[i]->val.s = strdup_chk(prog, "labelhash.key[].val");
+        keys[i]->val.s = strdup_chk(prog, "labelhash.key.val");
+        i++;
     }
-    keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key[]");
+    keys[i] = malloc_chk(sizeof(HKEY), "labelhash.key");
     keys[i]->type = CHARS;
-    keys[i]->val.s = strdup_chk(label, "labelhash.key[].val");
-    /* ハッシュ値を返す */
-    return hash(i+1, keys, LABELTABSIZE);
+    keys[i]->val.s = strdup_chk(label, "labelhash.key.val");
+    h = hash(i+1, keys, LABELTABSIZE);
+    for(j = 0; j < i + 1; j++) {
+        FREE(keys[j]->val.s);
+        FREE(keys[j]);
+    }
+    return h;
 }
 
 /**
- * ã\83©ã\83\99ã\83«è¡¨ã\81\8bã\82\89ã\82¢ã\83\89ã\83¬ã\82¹ã\82\92検索する
+ * ã\83\97ã\83­ã\82°ã\83©ã\83 å\90\8dã\81¨ã\83©ã\83\99ã\83«ã\81«å¯¾å¿\9cã\81\99ã\82\8bã\82¢ã\83\89ã\83¬ã\82¹ã\82\92ã\83©ã\83\99ã\83«è¡¨ã\81\8bã\82\89検索する
  */
 WORD getlabel(const char *prog, const char *label)
 {
     assert(label != NULL);
-    LABELTAB *np;
+    LABELTAB *p;
 
-    for(np = labels[labelhash(prog, label)]; np != NULL; np = np->next) {
-        if((prog == NULL || (np->prog != NULL && strcmp(prog, np->prog) == 0)) &&
-           strcmp(label, np->label) == 0)
+    for(p = labels[labelhash(prog, label)]; p != NULL; p = p->next) {
+        if((prog == NULL || (p->prog != NULL && strcmp(prog, p->prog) == 0)) &&
+           strcmp(label, p->label) == 0)
         {
-            return np->adr;
+            return p->adr;
         }
     }
     return 0xFFFF;
@@ -104,20 +104,20 @@ int compare_adr(const void *a, const void *b)
 void printlabel()
 {
     int i, asize = 0;
-    LABELTAB *np;
+    LABELTAB *p;
     LABELARRAY *ar[labelcnt];
 
     for(i = 0; i < LABELTABSIZE; i++) {
-        for(np = labels[i]; np != NULL; np = np->next) {
-            assert(np->label != NULL);
+        for(p = labels[i]; p != NULL; p = p->next) {
+            assert(p->label != NULL);
             ar[asize] = malloc_chk(sizeof(LABELARRAY), "ar[]");
-            if(np->prog == NULL) {
+            if(p->prog == NULL) {
                 ar[asize]->prog = NULL;
             } else {
-                ar[asize]->prog = strdup_chk(np->prog, "ar[].prog");
+                ar[asize]->prog = strdup_chk(p->prog, "ar[].prog");
             }
-            ar[asize]->label = strdup_chk(np->label, "ar[].label");
-            ar[asize++]->adr = np->adr;
+            ar[asize]->label = strdup_chk(p->label, "ar[].label");
+            ar[asize++]->adr = p->adr;
         }
     }
     qsort(ar, asize, sizeof(*ar), compare_adr);
@@ -135,16 +135,14 @@ void printlabel()
 void freelabel()
 {
     int i;
-    LABELTAB *np, *nq;
+    LABELTAB *p, *q;
 
     for(i = 0; i < LABELTABSIZE; i++) {
-        for(np = labels[i]; np != NULL; np = nq) {
-            nq = np->next;
-            if(np->prog != NULL) {
-                free_chk(np->prog, "np.prog");
-            }
-            free_chk(np->label, "np.label");
-            free_chk(np, "np");
+        for(p = labels[i]; p != NULL; p = q) {
+            q = p->next;
+            FREE(p->prog);
+            FREE(p->label);
+            FREE(p);
         }
     }
 }