]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
machines.c:
authorDamir Tomic <viperus@ntp.org>
Wed, 19 Aug 2015 22:23:42 +0000 (00:23 +0200)
committerDamir Tomic <viperus@ntp.org>
Wed, 19 Aug 2015 22:23:42 +0000 (00:23 +0200)
  Fixing bug 2898, replacing malloc with emalloc, calloc with emalloc_zero, strdup with estrup etc.
Many files:
  Fixing bug 2898, replacing malloc with emalloc, calloc with emalloc_zero, strdup with estrup etc.

bk: 55d501eeU564GNQIc_l35Hx9MHdIDg

17 files changed:
libntp/machines.c
ntpd/refclock_palisade.c
ntpq/ntpq.c
sntp/libevent/sample/http-server.c
sntp/libevent/test/bench_httpclient.c
sntp/libevent/test/regress.c
sntp/libevent/test/regress_dns.c
sntp/libevent/test/regress_http.c
sntp/libevent/test/regress_minheap.c
sntp/libevent/test/test-ratelim.c
sntp/libevent/test/test-time.c
sntp/libopts/compat/pathfind.c
sntp/tests/fileHandlingTest.c
sntp/tests/packetProcessing.c
tests/libntp/a_md5encrypt.c
tests/libntp/calendar.c
tests/libntp/caljulian.c

index 43944f9edb3f401fee2178b5c6e028d9213612a2..7a29ac07ea5fa6ff1f562205e0277050929bea8c 100644 (file)
@@ -40,7 +40,7 @@ struct hostent *gethostbyname(char *name)
        {
        struct hostent *host1;
        h_errno = 0;                                    /* we are always successful!!! */
-       host1 = (struct hostent *) malloc (sizeof(struct hostent));
+       host1 = (struct hostent *) emalloc (sizeof(struct hostent));
        host1->h_name = name;
        host1->h_addrtype = AF_INET;
        host1->h_aliases = name;
@@ -54,7 +54,7 @@ struct hostent *gethostbyaddr(char *name, int size, int addr_type)
        {
        struct hostent *host1;
        h_errno = 0;  /* we are always successful!!! */
-       host1 = (struct hostent *) malloc (sizeof(struct hostent));
+       host1 = (struct hostent *) emalloc (sizeof(struct hostent));
        host1->h_name = name;
        host1->h_addrtype = AF_INET;
        host1->h_aliases = name;
@@ -66,7 +66,7 @@ struct hostent *gethostbyaddr(char *name, int size, int addr_type)
 struct servent *getservbyname (char *name, char *type)
        {
        struct servent *serv1;
-       serv1 = (struct servent *) malloc (sizeof(struct servent));
+       serv1 = (struct servent *) emalloc (sizeof(struct servent));
        serv1->s_name = "ntp";      /* official service name */
        serv1->s_aliases = NULL;        /* alias list */
        serv1->s_port = 123;            /* port # */
index 0520311d1bc7ac88292b299d0c1db9513c3e1297..921c815ce163a751b789ff782ba20746251c233f 100644 (file)
@@ -218,7 +218,7 @@ init_thunderbolt (
        struct packettx tx;
        
        tx.size = 0;
-       tx.data = (u_char *) malloc(100);
+       tx.data = (u_char *) emalloc(100);
 
        /* set UTC time */
        sendsupercmd (&tx, 0x8E, 0xA2);
@@ -246,7 +246,7 @@ init_acutime (
        struct packettx tx;
 
        tx.size = 0;
-       tx.data = (u_char *) malloc(100);
+       tx.data = (u_char *) emalloc(100);
 
        sendsupercmd(&tx, 0x8E, 0xA5);
        sendbyte(&tx, 0x02);
index ee853f985cd2739bc88eab9b980946fb2c73ba21..17fe2ea45d5b756b48e85e4833211cf7158af6dd 100644 (file)
@@ -489,7 +489,7 @@ ntpqmain(
            builtins[icmd].desc[0] = "md5";
            fmt = "set key type to use for authenticated requests (%s)";
 #endif
-           msg = malloc(strlen(fmt) + strlen(list) - strlen("%s") +1);
+           msg = emalloc(strlen(fmt) + strlen(list) - strlen("%s") +1);
            sprintf(msg, fmt, list);
            builtins[icmd].comment = msg;
            free(list);
@@ -3502,7 +3502,7 @@ static void list_md_fn(const EVP_MD *m, const char *from, const char *to, void *
         if (!strcmp(*seen, name))
            return;
     n = (seen - hstate->seen) + 2;
-    hstate->seen = realloc(hstate->seen, n * sizeof(*seen));
+    hstate->seen = erealloc(hstate->seen, n * sizeof(*seen));
     hstate->seen[n-2] = name;
     hstate->seen[n-1] = NULL;
 
@@ -3520,10 +3520,10 @@ static void list_md_fn(const EVP_MD *m, const char *from, const char *to, void *
     len += (hstate->idx >= K_PER_LINE)? strlen(K_NL_PFX_STR): strlen(K_DELIM_STR);
 
     if (hstate->list == NULL) {
-       hstate->list = (char *)malloc(len);
+       hstate->list = (char *)emalloc(len);
        hstate->list[0] = '\0';
     } else
-       hstate->list = (char *)realloc(hstate->list, len);
+       hstate->list = (char *)erealloc(hstate->list, len);
 
     sprintf(hstate->list + strlen(hstate->list), "%s%s",
            ((hstate->idx >= K_PER_LINE)? K_NL_PFX_STR : K_DELIM_STR),
@@ -3544,18 +3544,18 @@ static char *list_digest_names(void)
 # ifdef HAVE_EVP_MD_DO_ALL_SORTED
     struct hstate hstate = { NULL, NULL, K_PER_LINE+1 };
 
-    hstate.seen = (const char **)calloc(1, sizeof( const char * ));
+    hstate.seen = (const char **) emalloc_zero(1*sizeof( const char * )); // replaces -> calloc(1, sizeof( const char * ));
 
     INIT_SSL();
     EVP_MD_do_all_sorted(list_md_fn, &hstate);
     list = hstate.list;
     free(hstate.seen);
 # else
-    list = (char *)malloc(sizeof("md5, others (upgrade to OpenSSL-1.0 for full list)"));
+    list = (char *)emalloc(sizeof("md5, others (upgrade to OpenSSL-1.0 for full list)"));
     strcpy(list, "md5, others (upgrade to OpenSSL-1.0 for full list)");
 # endif
 #else
-    list = (char *)malloc(sizeof("md5"));
+    list = (char *)emalloc(sizeof("md5"));
     strcpy(list, "md5");
 #endif
 
index cbb9c914dd8fd9e758419d6a5a43c5674cfe640e..e3615cf8366189e475832a89943f5b4c24eeac22 100644 (file)
@@ -200,7 +200,7 @@ send_document_cb(struct evhttp_request *req, void *arg)
                goto err;
 
        len = strlen(decoded_path)+strlen(docroot)+2;
-       if (!(whole_path = malloc(len))) {
+       if (!(whole_path = emalloc(len))) {
                perror("malloc");
                goto err;
        }
@@ -232,7 +232,7 @@ send_document_cb(struct evhttp_request *req, void *arg)
 
 #ifdef _WIN32
                dirlen = strlen(whole_path);
-               pattern = malloc(dirlen+3);
+               pattern = emalloc(dirlen+3);
                memcpy(pattern, whole_path, dirlen);
                pattern[dirlen] = '\\';
                pattern[dirlen+1] = '*';
index 22f174da5b662640a835964b7fbb35f070f8c3ed..501484961d62b11a2e23b5b4bb03674f14d4333f 100644 (file)
@@ -158,7 +158,7 @@ launch_request(void)
                }
        }
 
-       ri = malloc(sizeof(*ri));
+       ri = emalloc(sizeof(*ri));
        ri->n_read = 0;
        evutil_gettimeofday(&ri->started, NULL);
 
index 399ba2fbd8d7953025882865d744f1b8d479715e..4f1f888596fc626086cd4cc9a2169ee960ba167c 100644 (file)
@@ -2822,7 +2822,7 @@ check_dummy_mem_ok(void *mem_)
 static void *
 dummy_malloc(size_t len)
 {
-       char *mem = malloc(len+16);
+       char *mem = emalloc(len+16);
        memcpy(mem, "{[<guardedram>]}", 16);
        return mem+16;
 }
@@ -2835,7 +2835,7 @@ dummy_realloc(void *mem_, size_t len)
                return dummy_malloc(len);
        tt_want(check_dummy_mem_ok(mem_));
        mem -= 16;
-       mem = realloc(mem, len+16);
+       mem = erealloc(mem, len+16);
        return mem+16;
 }
 
index 6d6b48443df591ba1414c0dd4a4c93a695994911..e900da004ea320e9931c5b2f43446749fddfe9d3 100644 (file)
@@ -1746,7 +1746,7 @@ end:
 static void
 gaic_launch(struct event_base *base, struct evdns_base *dns_base)
 {
-       struct gaic_request_status *status = calloc(1,sizeof(*status));
+       struct gaic_request_status *status = emalloc_zero(1 * sizeof(*status)); //calloc
        struct timeval tv = { 0, 10000 };
        status->magic = GAIC_MAGIC;
        status->base = base;
@@ -1771,7 +1771,7 @@ static void *
 cnt_malloc(size_t sz)
 {
        allocated_chunks += 1;
-       return malloc(sz);
+       return emalloc(sz);
 }
 
 static void *
@@ -1781,7 +1781,7 @@ cnt_realloc(void *old, size_t sz)
                allocated_chunks += 1;
        if (!sz)
                allocated_chunks -= 1;
-       return realloc(old, sz);
+       return erealloc(old, sz);
 }
 
 static void
@@ -1815,7 +1815,7 @@ testleak_setup(const struct testcase_t *testcase)
        event_enable_debug_mode();
 
        /* not mm_calloc: we don't want to mess with the count. */
-       env = calloc(1, sizeof(struct testleak_env_t));
+       env = emalloc_zero(1 * sizeof(struct testleak_env_t)); //calloc
        env->base = event_base_new();
        env->dns_base = evdns_base_new(env->base, 0);
        env->req = evdns_base_resolve_ipv4(
index 35f6dd768961c3bb3ea1c8a7ab01b336c35c26b7..8cd830519787fb4118cacdb1a048f6a123b03065 100644 (file)
@@ -365,7 +365,7 @@ static void
 http_chunked_cb(struct evhttp_request *req, void *arg)
 {
        struct timeval when = { 0, 0 };
-       struct chunk_req_state *state = malloc(sizeof(struct chunk_req_state));
+       struct chunk_req_state *state = emalloc(sizeof(struct chunk_req_state));
        event_debug(("%s: called\n", __func__));
 
        memset(state, 0, sizeof(struct chunk_req_state));
@@ -844,7 +844,7 @@ http_allowed_methods_eventcb(struct bufferevent *bev, short what, void *arg)
                        buf[n]='\0';
                        if (*output)
                                free(*output);
-                       *output = strdup(buf);
+                       *output = estrdup(buf);
                }
                event_base_loopexit(exit_base, NULL);
        }
@@ -2643,7 +2643,7 @@ http_uriencode_test(void *ptr)
        s = NULL;
 
        /* Now try decoding just part of string. */
-       s = malloc(6 + 1 /* NUL byte */);
+       s = emalloc(6 + 1 /* NUL byte */);
        bytes_decoded = evhttp_decode_uri_internal("hello%20%20", 6, s, 0);
        tt_assert(s);
        tt_int_op(bytes_decoded,==,6);
index 05db32e26f4de7420ea8d9dd60392e2acc224279..97dee163731a45204cd7f9629ec5a764b585a31b 100644 (file)
@@ -62,7 +62,7 @@ test_heap_randomized(void *ptr)
        min_heap_ctor_(&heap);
 
        for (i = 0; i < 1024; ++i) {
-               inserted[i] = malloc(sizeof(struct event));
+               inserted[i] = emalloc(sizeof(struct event));
                set_random_timeout(inserted[i]);
                min_heap_push_(&heap, inserted[i]);
        }
index 17babfdcbcf5804af022805849a2b395f78aa0c8..54afa276c381271e020265fad0144d13e5ac494c 100644 (file)
@@ -338,8 +338,8 @@ test_ratelimiting(void)
        if (expected_total_persec > 0)
                expected_total_persec /= seconds_per_tick;
 
-       bevs = calloc(cfg_n_connections, sizeof(struct bufferevent *));
-       states = calloc(cfg_n_connections, sizeof(struct client_state));
+       bevs = emalloc_zero(cfg_n_connections * sizeof(struct bufferevent *)); //calloc
+       states = emalloc_zero(cfg_n_connections * sizeof(struct client_state)); //calloc
 
        for (i = 0; i < cfg_n_connections; ++i) {
                bevs[i] = bufferevent_socket_new(base, -1,
index bcc7086df7eda470b47d641715926416bf46576d..36a7c7b0ddb2784db2f0b31d5d26decb5922a7d0 100644 (file)
@@ -98,7 +98,7 @@ main(int argc, char **argv)
        event_init();
 
        for (i = 0; i < NEVENT; i++) {
-               ev[i] = malloc(sizeof(struct event));
+               ev[i] = emalloc(sizeof(struct event));
 
                /* Initalize one event */
                evtimer_set(ev[i], time_cb, ev[i]);
index 5c477caf07a6261c0a768c375c97ab27d28d4ac4..5890ec62f537c4fa62df6e2360ad8eae8f4db813 100644 (file)
@@ -19,7 +19,7 @@ pathfind( char const * path,
           char const * fname,
           char const * mode )
 {
-    return strdup(fname);
+    return estrdup(fname);
 }
 #else
 
@@ -114,10 +114,10 @@ make_absolute( char const * string, char const * dot_path )
     int result_len;
 
     if (!dot_path || *string == '/') {
-        result = strdup( string );
+        result = estrdup( string );
     } else {
         if (dot_path && dot_path[0]) {
-            result = malloc( 2 + strlen( dot_path ) + strlen( string ) );
+            result = emalloc( 2 + strlen( dot_path ) + strlen( string ) );
             strcpy( result, dot_path );
             result_len = (int)strlen(result);
             if (result[result_len - 1] != '/') {
@@ -125,7 +125,7 @@ make_absolute( char const * string, char const * dot_path )
                 result[result_len] = '\0';
             }
         } else {
-            result = malloc( 3 + strlen( string ) );
+            result = emalloc( 3 + strlen( string ) );
             result[0] = '.'; result[1] = '/'; result[2] = '\0';
             result_len = 2;
         }
@@ -154,7 +154,7 @@ canonicalize_pathname( char *path )
     char stub_char, *result;
 
     /* The result cannot be larger than the input PATH. */
-    result = strdup( path );
+    result = estrdup( path );
 
     stub_char = (*path == '/') ? '/' : '.';
 
index 1e0f6a6a7b9512557714f1ffc9f992821fdc37cc..ce3f0de772818c8622ced4ad15a05ce153bd0e35 100644 (file)
@@ -19,7 +19,7 @@ enum DirectoryType {
 const char *
 CreatePath(const char* filename, enum DirectoryType argument) {
        const char srcdir[] = SRCDIR_DEF;//"@abs_srcdir@/data/";
-       char * path = malloc (sizeof (char) * (strlen(srcdir) + 256));
+       char * path = emalloc (sizeof (char) * (strlen(srcdir) + 256));
 
        //char cwd[1024];
 
index 51ec572f1b0d0edaf5fb7c7efb2e2ff77257580e..1fd649ea627cd89d58122f240c63c184cbaf7cf5 100644 (file)
@@ -52,7 +52,7 @@ PrepareAuthenticationTest(int key_id,
        ActivateOption("-a", str);
 
        key_cnt = 1;
-       key_ptr = malloc(sizeof(struct key));
+       key_ptr = emalloc(sizeof(struct key));
        key_ptr->next = NULL;
        key_ptr->key_id = key_id;
        key_ptr->key_len = key_len;
index 89f8cb8dd753d1fd0b2515e708e880f480cfc816..f46376efd343767af9f657e0ea3453802e70b97b 100644 (file)
@@ -38,7 +38,7 @@ test_Encrypt(void) {
        char *packetPtr;
        int length;
 
-       packetPtr = malloc(totalLength * sizeof(*packetPtr));
+       packetPtr = emalloc(totalLength * sizeof(*packetPtr));
 
        memset(packetPtr + packetLength, 0, keyIdLength);
        memcpy(packetPtr, packet, packetLength);
index 80fcd2c377df18276aa5e56fce2abbfc53f1b5ba..2d4578d0dd9576db2543cba3c4817a9fb8247a2e 100644 (file)
@@ -75,7 +75,7 @@ leapdays(int year)
 
 char *
 CalendarFromCalToString(const struct calendar cal) {
-       char * str = malloc (sizeof (char) * 100);
+       char * str = emalloc (sizeof (char) * 100);
        
        char buffer[100] ="";
        snprintf(buffer, 100, "%u", cal.year);
@@ -105,7 +105,7 @@ CalendarFromCalToString(const struct calendar cal) {
 char *
 CalendarFromIsoToString(const struct isodate iso) {
 
-       char * str = malloc (sizeof (char) * 100);
+       char * str = emalloc (sizeof (char) * 100);
        
        char buffer[100] ="";
        snprintf(buffer, 100, "%u", iso.year);
@@ -162,7 +162,7 @@ IsEqualIso(const struct isodate expected, const struct isodate actual) {
 char *
 DateFromCalToString(const struct calendar cal) {
 
-       char * str = malloc (sizeof (char) * 100);
+       char * str = emalloc (sizeof (char) * 100);
        
        char buffer[100] ="";
        snprintf(buffer, 100, "%u", cal.year);
@@ -184,7 +184,7 @@ DateFromCalToString(const struct calendar cal) {
 char *
 DateFromIsoToString(const struct isodate iso) {
 
-       char * str = malloc (sizeof (char) * 100);
+       char * str = emalloc (sizeof (char) * 100);
        
        char buffer[100] ="";
        snprintf(buffer, 100, "%u", iso.year);
index 4221694547b4ae0439f77486003c47d2ceac60e4..ecf5d9255620801d63945a7af9a8fcccd6b9946e 100644 (file)
@@ -21,7 +21,7 @@ void test_uLongWrapped(void);
 
 char *
 CalendarToString(const struct calendar cal) {
-       char * str = malloc (sizeof (char) * 100);
+       char * str = emalloc (sizeof (char) * 100);
        
        char buffer[100] ="";
        snprintf(buffer, 100, "%u", cal.year);