registry: prevent a memory leak when loading a saved database (was allocating the same URL multiple times)
Costa Tsaousis (ktsaou) committed
Sep 4, 2016 at 14:43 UTC
164d167cc5e61f52d5ac3dfad204efc79771619a
1 file changed
+26
-7
src/registry.c
+26
-7
@@ -328,10 +328,8 @@ static inline URL *registry_url_allocate_nolock(const char *url, size_t urllen)
328
return u;
329
}
330
331
-static inline URL *registry_url_get(const char *url, size_t urllen) {
332
- debug(D_REGISTRY, "Registry: registry_url_get('%s')", url);
333
-
334
- registry_urls_lock();
331
+static inline URL *registry_url_get_nolock(const char *url, size_t urllen) {
332
+ debug(D_REGISTRY, "Registry: registry_url_get_nolock('%s')", url);
333
334
URL *u = dictionary_get(registry.urls, url);
335
if(!u) {
@@ -339,6 +337,16 @@ static inline URL *registry_url_get(const char *url, size_t urllen) {
337
registry.urls_count++;
338
}
339
340
+ return u;
341
+}
342
+
343
+static inline URL *registry_url_get(const char *url, size_t urllen) {
344
+ debug(D_REGISTRY, "Registry: registry_url_get('%s')", url);
345
+
346
+ registry_urls_lock();
347
+
348
+ URL *u = registry_url_get_nolock(url, urllen);
349
+
350
registry_urls_unlock();
351
352
return u;
@@ -782,6 +790,7 @@ int registry_log_load(void) {
790
else
791
registry_request_delete(p->guid, machine_guid, url, name, when);
792
793
+ registry.log_count++;
794
break;
795
796
default:
@@ -1393,6 +1402,8 @@ int registry_save(void) {
1402
return -2;
1403
}
1404
1405
+ error_log_limit_unlimited();
1406
+
1407
char tmp_filename[FILENAME_MAX + 1];
1408
char old_filename[FILENAME_MAX + 1];
1409
@@ -1404,6 +1415,7 @@ int registry_save(void) {
1415
if(!fp) {
1416
error("Registry: Cannot create file: %s", tmp_filename);
1417
registry_log_unlock();
1418
+ error_log_limit_reset();
1419
return -1;
1420
}
1421
@@ -1415,6 +1427,7 @@ int registry_save(void) {
1427
error("Registry: Cannot save registry machines - return value %d", bytes1);
1428
fclose(fp);
1429
registry_log_unlock();
1430
+ error_log_limit_reset();
1431
return bytes1;
1432
}
1433
debug(D_REGISTRY, "Registry: saving machines took %d bytes", bytes1);
@@ -1425,6 +1438,7 @@ int registry_save(void) {
1438
error("Registry: Cannot save registry persons - return value %d", bytes2);
1439
fclose(fp);
1440
registry_log_unlock();
1441
+ error_log_limit_reset();
1442
return bytes2;
1443
}
1444
debug(D_REGISTRY, "Registry: saving persons took %d bytes", bytes2);
@@ -1478,13 +1492,13 @@ int registry_save(void) {
1492
// it has been moved successfully
1493
// discard the current registry log
1494
registry_log_recreate_nolock();
1481
-
1495
registry.log_count = 0;
1496
}
1497
}
1498
1499
// continue operations
1500
registry_log_unlock();
1501
+ error_log_limit_reset();
1502
1503
return -1;
1504
}
@@ -1576,7 +1590,8 @@ static inline size_t registry_load(void) {
1590
}
1591
*url++ = '\0';
1592
1579
- u = registry_url_allocate_nolock(url, strlen(url));
1593
+ // u = registry_url_allocate_nolock(url, strlen(url));
1594
+ u = registry_url_get_nolock(url, strlen(url));
1595
1596
time_t first_t = strtoul(&s[2], NULL, 16);
1597
@@ -1603,7 +1618,8 @@ static inline size_t registry_load(void) {
1618
}
1619
1620
s[1] = s[10] = s[19] = s[28] = s[31] = '\0';
1606
- u = registry_url_allocate_nolock(&s[32], strlen(&s[32]));
1621
+ // u = registry_url_allocate_nolock(&s[32], strlen(&s[32]));
1622
+ u = registry_url_get_nolock(&s[32], strlen(&s[32]));
1623
1624
MACHINE_URL *mu = registry_machine_url_allocate(m, u, strtoul(&s[2], NULL, 16));
1625
mu->last_t = strtoul(&s[11], NULL, 16);
@@ -1702,6 +1718,9 @@ int registry_init(void) {
1718
registry_log_open_nolock();
1719
registry_load();
1720
registry_log_load();
1721
+
1722
+ if(unlikely(registry_should_save_db()))
1723
+ registry_save();
1724
}
1725
1726
return 0;