]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 1400] An empty KOD DB file causes sntp to coredump.
authorDave Hart <hart@ntp.org>
Sat, 28 Nov 2009 06:31:50 +0000 (06:31 +0000)
committerDave Hart <hart@ntp.org>
Sat, 28 Nov 2009 06:31:50 +0000 (06:31 +0000)
bk: 4b10c3d6g8otqkLyi-P9bi-OJySHjg

ChangeLog
sntp/kod_management.c

index 684bd850a4c71ff51eb5a2fe4281010749bc32b2..8f364d0347a6e3a3dbb55ea52a9c3de445e38f42 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+* [Bug 1400] An empty KOD DB file causes sntp to coredump.
 (4.2.5p248-RC) 2009/11/26 Released by Harlan Stenn <stenn@ntp.org>
 * Prepare for the generation of sntp.html.
 * Documentation changes from Dave Mills.
index b94ffeb287ae47b11724161d97356f980b0fb730..d7aca5200f9cab6ee53f6ba131e2c3b9eab2c3f9 100644 (file)
@@ -53,19 +53,35 @@ add_entry(
        )
 {
        int n;
+       struct kod_entry *pke;
 
-       n = kod_db_cnt++;
-       kod_db = erealloc(kod_db, kod_db_cnt * sizeof(kod_db[0]));
-       kod_db[n] = emalloc(sizeof(*kod_db[n]));
+       pke = emalloc(sizeof(*pke));
+       pke->timestamp = time(NULL);
+       memcpy(pke->type, type, 4);
+       pke->type[sizeof(pke->type) - 1] = '\0';
+       strncpy(pke->hostname, hostname,
+               sizeof(pke->hostname));
+       pke->hostname[sizeof(pke->hostname) - 1] = '\0';
 
-       kod_db[n]->timestamp = time(NULL);
+       /*
+        * insert in address ("hostname") order to find duplicates
+        */
+       for (n = 0; n < kod_db_cnt; n++)
+               if (strcmp(kod_db[n]->hostname, pke->hostname) >= 0)
+                       break;
 
-       memcpy(kod_db[n]->type, type, 4);
-       kod_db[n]->type[sizeof(kod_db[n]->type) - 1] = '\0';
+       if (n < kod_db_cnt &&
+           0 == strcmp(kod_db[n]->hostname, pke->hostname)) {
+               kod_db[n]->timestamp = pke->timestamp;
+               return;
+       }
 
-       strncpy(kod_db[n]->hostname, hostname,
-               sizeof(kod_db[n]->hostname));
-       kod_db[n]->hostname[sizeof(kod_db[n]->hostname) - 1] = '\0';
+       kod_db_cnt++;
+       kod_db = erealloc(kod_db, kod_db_cnt * sizeof(kod_db[0]));
+       if (n != kod_db_cnt - 1)
+               memmove(&kod_db[n + 1], &kod_db[n],
+                       sizeof(kod_db[0]) * ((kod_db_cnt - 1) - n));
+       kod_db[n] = pke;
 }
 
 
@@ -158,7 +174,7 @@ kod_init_kod_db(
         */
        char fbuf[254+10+4+2+1+1];
        FILE *db_s;
-       int a, b, sepc;
+       int a, b, sepc, len;
        unsigned long long ull;
        char *str_ptr;
        char error = 0;
@@ -187,19 +203,19 @@ kod_init_kod_db(
                return;
        }
 
-       if(ENABLED_OPT(NORMALVERBOSE))
+       if (ENABLED_OPT(NORMALVERBOSE))
                printf("Starting to read KoD file %s...\n", db_file);
        /* First let's see how many entries there are and check for right syntax */
 
-       while(!feof(db_s)) {
-               fgets(fbuf, sizeof(fbuf), db_s);
+       while (!feof(db_s) && NULL != fgets(fbuf, sizeof(fbuf), db_s)) {
 
                /* ignore blank lines */
                if ('\n' == fbuf[0])
                        continue;
 
                sepc = 0;
-               for(a = 0; a < strlen(fbuf); a++) {
+               len = strlen(fbuf);
+               for (a = 0; a < len; a++) {
                        if (' ' == fbuf[a])
                                sepc++;
 
@@ -207,27 +223,31 @@ kod_init_kod_db(
                                if (sepc != 2) {
                                        if (strcmp(db_file, "/dev/null")) {
                                                char msg[80];
-                                               snprintf(msg, sizeof(msg), "Syntax error in KoD db file %s in line %i (missing space)", db_file, (kod_db_cnt + 1));
-
+                                               snprintf(msg, sizeof(msg),
+                                                        "Syntax error in KoD db file %s in line %i (missing space)",
+                                                        db_file, kod_db_cnt + 1);
        #ifdef DEBUG
                                                debug_msg(msg);
                                                printf("%s\n", msg);
        #endif
-
                                                log_msg(msg, 1);
                                        }
                                        fclose(db_s);
-
                                        return;
                                }
-
                                sepc = 0;
                                kod_db_cnt++;
                        }
                }
        }
 
-       kod_db_cnt--;
+       if (0 == kod_db_cnt) {
+#ifdef DEBUG
+               printf("KoD DB %s empty.\n", db_file);
+#endif
+               fclose(db_s);
+               return;
+       }
 
 #ifdef DEBUG
        printf("KoD DB %s contains %d entries, reading...\n", db_file, kod_db_cnt);
@@ -238,7 +258,9 @@ kod_init_kod_db(
        kod_db = emalloc(sizeof(kod_db[0]) * kod_db_cnt);
 
        /* Read contents of file */
-       for(b=0; !feof(db_s) && !ferror(db_s) && b < kod_db_cnt; b++) {
+       for (b = 0; 
+            !feof(db_s) && !ferror(db_s) && b < kod_db_cnt;
+            b++) {
 
                str_ptr = fgets(fbuf, sizeof(fbuf), db_s);
                if (NULL == str_ptr) {
@@ -269,6 +291,7 @@ kod_init_kod_db(
        if (ferror(db_s) || error) {
                char msg[80];
 
+               kod_db_cnt = b;
                snprintf(msg, sizeof(msg), "An error occured while parsing the KoD db file %s", db_file);
 #ifdef DEBUG
                debug_msg(msg);
@@ -279,16 +302,12 @@ kod_init_kod_db(
                return;
        }
 
+       fclose(db_s);
 #ifdef DEBUG
        for (a = 0; a < kod_db_cnt; a++)
                printf("KoD entry %d: %s at %llx type %s\n", a,
                       kod_db[a]->hostname,
                       (unsigned long long)kod_db[a]->timestamp,
                       kod_db[a]->type);
-
-       printf("\n");
 #endif
-
-
-       fclose(db_s);
 }