From: Dave Hart Date: Fri, 24 Jul 2009 06:41:17 +0000 (+0000) Subject: [Bug 965] CID 42: ss_family uninitialized. X-Git-Tag: NTP_4_2_5P192~1^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=edad54ed3d0d591ac583b0720ccbf336d9fafaa6;p=thirdparty%2Fntp.git [Bug 965] CID 42: ss_family uninitialized. [Bug 1250] CID 53: kod_init_kod_db() overruns kod_db malloc'd buffer. [Bug 1251] CID 68: search_entry() mishandles dst argument. [Bug 1252] CID 32: Quiet Coverity warning with assertion. [Bug 1253] CID 50: gsoc_sntp/crypto.c auth_init() always returns a list with one entry. [Bug 1254] CID 56: tv_to_str() leaks a struct tm each call. [Bug 1255] CID 55: pkt_output() leaks a copy of each packet. [Bug 1256] CID 51: Coverity doesn't recognize our assertion macros as terminal. [Bug 1257] CID 57: gsoc_sntp auth_init() fails to fclose(keyfile). [Bug 1258] CID 54: gsoc_sntp resolve_hosts() needs simplification. [Bug 1259] CID 59: gsoc_sntp recv_bcast_data() fails to free(rdata) on error paths. [Bug 1260] CID 60: gsoc_sntp recvpkt() fails to free(rdata). bk: 4a69578dPevRCr_Tbm2D5PQv6EEfQQ --- diff --git a/ChangeLog b/ChangeLog index 4ab99d8d1..d05195e0e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +* [Bug 965] CID 42: ss_family uninitialized. +* [Bug 1250] CID 53: kod_init_kod_db() overruns kod_db malloc'd buffer. +* [Bug 1251] CID 68: search_entry() mishandles dst argument. +* [Bug 1252] CID 32: Quiet Coverity warning with assertion. +* [Bug 1253] CID 50: gsoc_sntp/crypto.c auth_init() always returns a + list with one entry. +* [Bug 1254] CID 56: tv_to_str() leaks a struct tm each call. +* [Bug 1255] CID 55: pkt_output() leaks a copy of each packet. +* [Bug 1256] CID 51: Coverity doesn't recognize our assertion macros as + terminal. +* [Bug 1257] CID 57: gsoc_sntp auth_init() fails to fclose(keyfile). +* [Bug 1258] CID 54: gsoc_sntp resolve_hosts() needs simplification. +* [Bug 1259] CID 59: gsoc_sntp recv_bcast_data() fails to free(rdata) + on error paths. +* [Bug 1260] CID 60: gsoc_sntp recvpkt() fails to free(rdata). (4.2.5p191) 2009/07/21 Released by Harlan Stenn * Updated to AutoGen-5.9.9pre1. (4.2.5p190) 2009/07/20 Released by Harlan Stenn diff --git a/gsoc_sntp/crypto.c b/gsoc_sntp/crypto.c index acf48a0b0..5867e0c06 100644 --- a/gsoc_sntp/crypto.c +++ b/gsoc_sntp/crypto.c @@ -90,6 +90,7 @@ auth_init ( if(feof(keyf)) { if(ENABLED_OPT(NORMALVERBOSE)) printf("sntp auth_init: Key file %s is empty!\n", keyfile); + fclose(keyf); return -1; } @@ -99,9 +100,9 @@ auth_init ( struct key *act = (struct key *) malloc(sizeof(struct key)); line_limit = 0; - fgets(kbuf, 96, keyf); + fgets(kbuf, sizeof(kbuf), keyf); - for(a=0; akey_id, &act->type, act->key_seq)) == 3) { act->key_len = strlen(act->key_seq); - - if(act->type != 0) { + act->next = NULL; + + if (NULL == prev) *keys = act; - prev = act; - } - else { + else prev->next = act; - act->next = NULL; - prev = act; - } + prev = act; key_cnt++; diff --git a/gsoc_sntp/kod_management.c b/gsoc_sntp/kod_management.c index 51ce8b55e..a172fb90b 100644 --- a/gsoc_sntp/kod_management.c +++ b/gsoc_sntp/kod_management.c @@ -39,28 +39,41 @@ search_entry ( ) { register int a, b, resc = 0; - struct kod_entry *sptr = kod_db; + struct kod_entry *sptr; - for(a=0; ahostname, hostname)) + sptr = kod_db; + for (a = 0; a < entryc && sptr; a++) { + if (!strcmp(sptr->hostname, hostname)) resc++; sptr = sptr->next; } - dst = (struct kod_entry **) malloc(sizeof(struct kod_entry) * resc); + if (!resc) + return 0; + + *dst = malloc(sizeof(struct kod_entry) * resc); + if (NULL == *dst) + return 0; - b=0; - for(a=0; ahostname, hostname)) { - dst[b] = sptr; + sptr = kod_db; + b = 0; + for (a = 0; a < entryc && sptr; a++) { + if (!strcmp(sptr->hostname, hostname)) { + (*dst)[b] = *sptr; + (*dst)[b].next = &((*dst)[b + 1]); b++; } + + sptr = sptr->next; } + if (b) + (*dst)[b - 1].next = NULL; return resc; } +#if 0 /* presently useless */ int kod_entry_exists ( char *search_str @@ -74,6 +87,7 @@ kod_entry_exists ( else return 1; } +#endif void add_entry ( @@ -166,19 +180,18 @@ kod_init_kod_db ( const char *db_file ) { - if(kod_init) - return; - -#ifdef DEBUG - printf("Initializing KOD DB...\n"); -#endif - register int a, b; /* Max. of 255 characters for hostname, 10 for timestamp, 4 for kisscode, 2 for format : and 1 for \n */ char fbuf[272]; char error = 0; + if (kod_init) + return; + +#ifdef DEBUG + printf("Initializing KOD DB...\n"); +#endif db_s = fopen(db_file, "r"); @@ -200,9 +213,10 @@ kod_init_kod_db ( /* First let's see how many entries there are and check for right syntax */ while(!feof(db_s)) { - fgets(fbuf, 272, db_s); - int sepc = 0; + + fgets(fbuf, sizeof(fbuf), db_s); + for(a=0; a 0) - kod_db[b-1].next = &kod_db[b]; + break; } - } -#ifdef DEBUG - for(a=0; a 0) + kod_db[b-1].next = &kod_db[b]; + } - if(ferror(db_s) || error) { + if (ferror(db_s) || error) { char msg[80]; - snprintf(msg, 80, "An error occured while parsing the KOD db file %s", db_file); + snprintf(msg, sizeof(msg), "An error occured while parsing the KOD db file %s", db_file); #ifdef DEBUG debug_msg(msg); #endif - log_msg(msg, 2); return; } +#ifdef DEBUG + for(a=0; aai_addr, resh[c]->ai_addrlen, adr_buf, sizeof(adr_buf), NULL, 0, NI_NUMERICHOST); int kodc; char *hostname = addrinfo_to_str(resh[c]); - if((kodc = search_entry(hostname, reason)) == 0 && - is_reachable(resh[c])) { - int ow_ret = on_wire(resh[c]); - if(ow_ret < 0) { - printf("on_wire failed for server %s!\n", hostname); - } - else { - sync_data_suc = 1; + if ((kodc = search_entry(hostname, &reason)) == 0) { + if (is_reachable(resh[c])) { + int ow_ret = on_wire(resh[c]); + + if (ow_ret < 0) + printf("on_wire failed for server %s!\n", hostname); + else + sync_data_suc = 1; } - } - else { + } else { printf("KoD %i packages exists for %s, stopping any further communication.\n", kodc, adr_buf); - + free(reason); } freeaddrinfo(resh[c]); free(hostname); } + free(resh); return 0; } diff --git a/gsoc_sntp/networking.c b/gsoc_sntp/networking.c index 16e968deb..33b2d4f46 100644 --- a/gsoc_sntp/networking.c +++ b/gsoc_sntp/networking.c @@ -30,40 +30,41 @@ int resolve_hosts ( char **hosts, int hostc, - struct addrinfo **res, + struct addrinfo ***res, int pref_family ) { - register unsigned int a, b; - unsigned int entryc = 0; + register unsigned int a; + unsigned int resc; + struct addrinfo **tres; - if(hostc < 1) + if (hostc < 1 || NULL == res) return 0; - struct addrinfo ***tres = (struct addrinfo ***) malloc(sizeof(struct addrinfo **) * hostc); + tres = malloc(sizeof(struct addrinfo *) * hostc); - for(a=0; aai_next) { - entryc++; -#ifdef DEBUG + } else { +#ifdef DEBUG + for (dres = tres[resc]; dres; dres = dres->ai_next) { getnameinfo(dres->ai_addr, dres->ai_addrlen, adr_buf, sizeof(adr_buf), NULL, 0, NI_NUMERICHOST); STDLINE - printf("Resolv No.: %i Result of getaddrinfo for %s:\n", entryc, hosts[a]); + printf("Resolv No.: %i Result of getaddrinfo for %s:\n", resc, hosts[a]); printf("socktype: %i ", dres->ai_socktype); printf("protocol: %i ", dres->ai_protocol); printf("Prefered socktype: %i IP: %s\n", dres->ai_socktype, adr_buf); STDLINE -#endif } - } - } - -#ifdef DEBUG - printf("Retrieved %i DNS entries, continuing...\n", entryc); #endif - - /* Make a list of the addrinfo list entries, start by counting them */ - struct addrinfo **result = (struct addrinfo **) malloc(sizeof(struct addrinfo**) * entryc); - - for(a=0, b=0; aai_next == NULL) { - result[b] = *tres[a]; - } - else { - struct addrinfo *seek = *tres[a]; - - for(; bai_next; - } + resc++; } } -#ifdef DEBUG - for(a=0; aai_addr, result[a]->ai_addrlen, adr_buf, sizeof(adr_buf), NULL, 0, NI_NUMERICHOST); - - printf("%x: IP %s\n", (unsigned int) result[a], adr_buf); -#endif - - *res = (struct addrinfo *) malloc(sizeof(struct addrinfo *) * entryc); - - for(a=0; a 2); + * ... + * + * NTP_ENSURE(result != 12); + * return result; + * } + * + * open question: when would we use NTP_INVARIANT()? */ -#ifndef NTP_ASSSERT_H +#ifndef NTP_ASSERT_H #define NTP_ASSERT_H # ifdef CALYSTO extern void calysto_assume(unsigned char cnd); /* assume this always holds */ extern void calysto_assert(unsigned char cnd); /* check whether this holds */ -#define NTP_REQUIRE(x) calysto_assert(x) -#define NTP_INSIST(x) calysto_assume(x) -#define NTP_INVARIANT(x) calysto_assume(x) -#define NTP_ENSURE(x) calysto_assert(x) - -# else /* ~CALYSTO */ +#define NTP_REQUIRE(x) calysto_assert(x) +#define NTP_INSIST(x) calysto_assume(x) /* DLH calysto_assert()? */ +#define NTP_INVARIANT(x) calysto_assume(x) +#define NTP_ENSURE(x) calysto_assert(x) + +# elif defined(__COVERITY__) + +/* + * Coverity has special knowledge that assert(x) terminates the process + * if x is not true. Rather than teach it about our assertion macros, + * just use the one it knows about for Coverity Prevent scans. This + * means our assertion code (and ISC's) escapes Coverity analysis, but + * that seems to be a reasonable trade-off. + */ + +#define NTP_REQUIRE(x) assert(x) +#define NTP_INSIST(x) assert(x) +#define NTP_INVARIANT(x) assert(x) +#define NTP_ENSURE(x) assert(x) + +# else /* neither Coverity nor Calysto */ #include "isc/assertions.h" -#define NTP_REQUIRE(x) ISC_REQUIRE(x) -#define NTP_INSIST(x) ISC_INSIST(x) -#define NTP_INVARIANT(x) ISC_INVARIANT(x) -#define NTP_ENSURE(x) ISC_ENSURE(x) +#define NTP_REQUIRE(x) ISC_REQUIRE(x) +#define NTP_INSIST(x) ISC_INSIST(x) +#define NTP_INVARIANT(x) ISC_INVARIANT(x) +#define NTP_ENSURE(x) ISC_ENSURE(x) -# endif /* ~CALYSTO */ -#endif +# endif /* neither Coverity nor Calysto */ +#endif /* NTP_ASSERT_H */ diff --git a/ntpd/refclock_nmea.c b/ntpd/refclock_nmea.c index 8be9879a0..294791a2a 100644 --- a/ntpd/refclock_nmea.c +++ b/ntpd/refclock_nmea.c @@ -236,6 +236,7 @@ nmea_start( return(0); if ((p = getprotobyname("ip")) == NULL) return(0); + memset(&so_addr, 0, sizeof(so_addr)); so_addr.sin_family = AF_INET; so_addr.sin_port = htons(nmea_port); so_addr.sin_addr = *((struct in_addr *) he->h_addr); diff --git a/ntpdc/ntpdc.c b/ntpdc/ntpdc.c index 09c44afcf..78abce39a 100644 --- a/ntpdc/ntpdc.c +++ b/ntpdc/ntpdc.c @@ -12,6 +12,7 @@ #include "ntp_select.h" #include "ntp_io.h" #include "ntp_stdlib.h" +#include "ntp_assert.h" #include "ntp_lineedit.h" /* Don't include ISC's version of IPv6 variables and structures */ #define ISC_IPV6_H 1 @@ -536,6 +537,12 @@ openhost( return 0; } + /* + * getaddrinfo() has returned without error so ai should not + * be NULL. + */ + NTP_INSIST(ai != NULL); + if (ai->ai_canonname == NULL) { strncpy(temphost, stoa((sockaddr_u *)ai->ai_addr), LENHOSTNAME); @@ -612,8 +619,8 @@ openhost( ai->ai_addrlen) == -1) #endif /* SYS_VXWORKS */ error("connect", "", ""); - if (ai != NULL) - freeaddrinfo(ai); + + freeaddrinfo(ai); havehost = 1; req_pkt_size = REQ_LEN_NOMAC; impl_ver = IMPL_XNTPD; diff --git a/ports/winnt/libntp/libntp.vcproj b/ports/winnt/libntp/libntp.vcproj index 71a3d14bb..351bfbf0c 100644 --- a/ports/winnt/libntp/libntp.vcproj +++ b/ports/winnt/libntp/libntp.vcproj @@ -1966,6 +1966,10 @@ RelativePath="..\..\..\include\ntp.h" > + + diff --git a/ports/winnt/ntpd/ntpd.vcproj b/ports/winnt/ntpd/ntpd.vcproj index 6178fe5e8..2ad114e4a 100644 --- a/ports/winnt/ntpd/ntpd.vcproj +++ b/ports/winnt/ntpd/ntpd.vcproj @@ -808,6 +808,10 @@ RelativePath="..\..\..\include\ntp.h" > + + diff --git a/ports/winnt/ntpdc/ntpdc.vcproj b/ports/winnt/ntpdc/ntpdc.vcproj index 739e5833c..d3f6a3908 100644 --- a/ports/winnt/ntpdc/ntpdc.vcproj +++ b/ports/winnt/ntpdc/ntpdc.vcproj @@ -316,6 +316,10 @@ Name="Header Files" Filter="h;hpp;hxx;hm;inl" > + +