From: Damir Tomic Date: Wed, 19 Aug 2015 22:23:42 +0000 (+0200) Subject: machines.c: X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=de287e259bffc7358515da0368a7c79ce602424c;p=thirdparty%2Fntp.git machines.c: 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 --- diff --git a/libntp/machines.c b/libntp/machines.c index 43944f9ed..7a29ac07e 100644 --- a/libntp/machines.c +++ b/libntp/machines.c @@ -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 # */ diff --git a/ntpd/refclock_palisade.c b/ntpd/refclock_palisade.c index 0520311d1..921c815ce 100644 --- a/ntpd/refclock_palisade.c +++ b/ntpd/refclock_palisade.c @@ -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); diff --git a/ntpq/ntpq.c b/ntpq/ntpq.c index ee853f985..17fe2ea45 100644 --- a/ntpq/ntpq.c +++ b/ntpq/ntpq.c @@ -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 diff --git a/sntp/libevent/sample/http-server.c b/sntp/libevent/sample/http-server.c index cbb9c914d..e3615cf83 100644 --- a/sntp/libevent/sample/http-server.c +++ b/sntp/libevent/sample/http-server.c @@ -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] = '*'; diff --git a/sntp/libevent/test/bench_httpclient.c b/sntp/libevent/test/bench_httpclient.c index 22f174da5..501484961 100644 --- a/sntp/libevent/test/bench_httpclient.c +++ b/sntp/libevent/test/bench_httpclient.c @@ -158,7 +158,7 @@ launch_request(void) } } - ri = malloc(sizeof(*ri)); + ri = emalloc(sizeof(*ri)); ri->n_read = 0; evutil_gettimeofday(&ri->started, NULL); diff --git a/sntp/libevent/test/regress.c b/sntp/libevent/test/regress.c index 399ba2fbd..4f1f88859 100644 --- a/sntp/libevent/test/regress.c +++ b/sntp/libevent/test/regress.c @@ -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, "{[]}", 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; } diff --git a/sntp/libevent/test/regress_dns.c b/sntp/libevent/test/regress_dns.c index 6d6b48443..e900da004 100644 --- a/sntp/libevent/test/regress_dns.c +++ b/sntp/libevent/test/regress_dns.c @@ -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( diff --git a/sntp/libevent/test/regress_http.c b/sntp/libevent/test/regress_http.c index 35f6dd768..8cd830519 100644 --- a/sntp/libevent/test/regress_http.c +++ b/sntp/libevent/test/regress_http.c @@ -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); diff --git a/sntp/libevent/test/regress_minheap.c b/sntp/libevent/test/regress_minheap.c index 05db32e26..97dee1637 100644 --- a/sntp/libevent/test/regress_minheap.c +++ b/sntp/libevent/test/regress_minheap.c @@ -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]); } diff --git a/sntp/libevent/test/test-ratelim.c b/sntp/libevent/test/test-ratelim.c index 17babfdcb..54afa276c 100644 --- a/sntp/libevent/test/test-ratelim.c +++ b/sntp/libevent/test/test-ratelim.c @@ -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, diff --git a/sntp/libevent/test/test-time.c b/sntp/libevent/test/test-time.c index bcc7086df..36a7c7b0d 100644 --- a/sntp/libevent/test/test-time.c +++ b/sntp/libevent/test/test-time.c @@ -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]); diff --git a/sntp/libopts/compat/pathfind.c b/sntp/libopts/compat/pathfind.c index 5c477caf0..5890ec62f 100644 --- a/sntp/libopts/compat/pathfind.c +++ b/sntp/libopts/compat/pathfind.c @@ -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 == '/') ? '/' : '.'; diff --git a/sntp/tests/fileHandlingTest.c b/sntp/tests/fileHandlingTest.c index 1e0f6a6a7..ce3f0de77 100644 --- a/sntp/tests/fileHandlingTest.c +++ b/sntp/tests/fileHandlingTest.c @@ -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]; diff --git a/sntp/tests/packetProcessing.c b/sntp/tests/packetProcessing.c index 51ec572f1..1fd649ea6 100644 --- a/sntp/tests/packetProcessing.c +++ b/sntp/tests/packetProcessing.c @@ -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; diff --git a/tests/libntp/a_md5encrypt.c b/tests/libntp/a_md5encrypt.c index 89f8cb8dd..f46376efd 100644 --- a/tests/libntp/a_md5encrypt.c +++ b/tests/libntp/a_md5encrypt.c @@ -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); diff --git a/tests/libntp/calendar.c b/tests/libntp/calendar.c index 80fcd2c37..2d4578d0d 100644 --- a/tests/libntp/calendar.c +++ b/tests/libntp/calendar.c @@ -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); diff --git a/tests/libntp/caljulian.c b/tests/libntp/caljulian.c index 422169454..ecf5d9255 100644 --- a/tests/libntp/caljulian.c +++ b/tests/libntp/caljulian.c @@ -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);