- exit log routine is annotated as noreturn function.

- free memory leaks in config strlist and str2list insert functions.
- do not move unused argv variable after getopt.
- Remove unused if clause in testcode.


git-svn-id: file:///svn/unbound/trunk@4896 be551aaa-1e26-0410-a405-d3ace91eadb9
This commit is contained in:
Wouter Wijngaards 2018-09-13 08:58:21 +00:00
parent 225a6d9c6e
commit 9a82526b91
15 changed files with 114 additions and 26 deletions

View file

@ -1,5 +1,8 @@
/* config.h.in. Generated from configure.ac by autoheader. */ /* config.h.in. Generated from configure.ac by autoheader. */
/* apply the noreturn attribute to a function that exits the program */
#undef ATTR_NORETURN
/* Directory to chroot to */ /* Directory to chroot to */
#undef CHROOT_DIR #undef CHROOT_DIR
@ -45,6 +48,9 @@
/* Whether the C compiler accepts the "format" attribute */ /* Whether the C compiler accepts the "format" attribute */
#undef HAVE_ATTR_FORMAT #undef HAVE_ATTR_FORMAT
/* Whether the C compiler accepts the "noreturn" attribute */
#undef HAVE_ATTR_NORETURN
/* Whether the C compiler accepts the "unused" attribute */ /* Whether the C compiler accepts the "unused" attribute */
#undef HAVE_ATTR_UNUSED #undef HAVE_ATTR_UNUSED

45
configure vendored
View file

@ -6269,6 +6269,51 @@ $as_echo "#define HAVE_ATTR_WEAK 1" >>confdefs.h
fi fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether the C compiler (${CC-cc}) accepts the \"noreturn\" attribute" >&5
$as_echo_n "checking whether the C compiler (${CC-cc}) accepts the \"noreturn\" attribute... " >&6; }
if ${ac_cv_c_noreturn_attribute+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_cv_c_noreturn_attribute=no
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
#include <stdio.h>
__attribute__((noreturn)) void f(int x) { printf("%d", x); }
int
main ()
{
f(1);
;
return 0;
}
_ACEOF
if ac_fn_c_try_compile "$LINENO"; then :
ac_cv_c_noreturn_attribute="yes"
else
ac_cv_c_noreturn_attribute="no"
fi
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_c_noreturn_attribute" >&5
$as_echo "$ac_cv_c_noreturn_attribute" >&6; }
if test $ac_cv_c_noreturn_attribute = yes; then
$as_echo "#define HAVE_ATTR_NORETURN 1" >>confdefs.h
$as_echo "#define ATTR_NORETURN __attribute__((__noreturn__))" >>confdefs.h
fi
if test "$srcdir" != "."; then if test "$srcdir" != "."; then
CPPFLAGS="$CPPFLAGS -I$srcdir" CPPFLAGS="$CPPFLAGS -I$srcdir"
fi fi

View file

@ -311,11 +311,36 @@ __attribute__((weak)) void f(int x) { printf("%d", x); }
AC_MSG_RESULT($ac_cv_c_weak_attribute) AC_MSG_RESULT($ac_cv_c_weak_attribute)
if test $ac_cv_c_weak_attribute = yes; then if test $ac_cv_c_weak_attribute = yes; then
AC_DEFINE(HAVE_ATTR_WEAK, 1, [Whether the C compiler accepts the "weak" attribute]) AC_DEFINE(HAVE_ATTR_WEAK, 1, [Whether the C compiler accepts the "weak" attribute])
AC_DEFINE(ATTR_WEAK, [__attribute__((weak))], [apply the weak attribute to a symbol])
fi fi
])dnl End of CHECK_WEAK_ATTRIBUTE ])dnl End of CHECK_WEAK_ATTRIBUTE
CHECK_WEAK_ATTRIBUTE CHECK_WEAK_ATTRIBUTE
AC_DEFUN([CHECK_NORETURN_ATTRIBUTE],
[AC_REQUIRE([AC_PROG_CC])
AC_MSG_CHECKING(whether the C compiler (${CC-cc}) accepts the "noreturn" attribute)
AC_CACHE_VAL(ac_cv_c_noreturn_attribute,
[ac_cv_c_noreturn_attribute=no
AC_TRY_COMPILE(
[ #include <stdio.h>
__attribute__((noreturn)) void f(int x) { printf("%d", x); }
], [
f(1);
],
[ac_cv_c_noreturn_attribute="yes"],
[ac_cv_c_noreturn_attribute="no"])
])
AC_MSG_RESULT($ac_cv_c_noreturn_attribute)
if test $ac_cv_c_noreturn_attribute = yes; then
AC_DEFINE(HAVE_ATTR_NORETURN, 1, [Whether the C compiler accepts the "noreturn" attribute])
AC_DEFINE(ATTR_NORETURN, [__attribute__((__noreturn__))], [apply the noreturn attribute to a function that exits the program])
fi
])dnl End of CHECK_NORETURN_ATTRIBUTE
CHECK_NORETURN_ATTRIBUTE
if test "$srcdir" != "."; then if test "$srcdir" != "."; then
CPPFLAGS="$CPPFLAGS -I$srcdir" CPPFLAGS="$CPPFLAGS -I$srcdir"
fi fi

View file

@ -730,7 +730,7 @@ main(int argc, char* argv[])
} }
} }
argc -= optind; argc -= optind;
argv += optind; /* argv += optind; not using further arguments */
if(winopt) { if(winopt) {
#ifdef UB_ON_WINDOWS #ifdef UB_ON_WINDOWS

View file

@ -1,5 +1,9 @@
13 September 2018: Wouter 13 September 2018: Wouter
- Fix seed for random backup code to use explicit zero when wiped. - Fix seed for random backup code to use explicit zero when wiped.
- exit log routine is annotated as noreturn function.
- free memory leaks in config strlist and str2list insert functions.
- do not move unused argv variable after getopt.
- Remove unused if clause in testcode.
11 September 2018: Wouter 11 September 2018: Wouter
- Fixed unused return value warnings in contrib/fastrpz.patch for - Fixed unused return value warnings in contrib/fastrpz.patch for

View file

@ -109,13 +109,13 @@ static struct ub_ctx* ub_ctx_create_nopipe(void)
alloc_init(&ctx->superalloc, NULL, 0); alloc_init(&ctx->superalloc, NULL, 0);
seed = (unsigned int)time(NULL) ^ (unsigned int)getpid(); seed = (unsigned int)time(NULL) ^ (unsigned int)getpid();
if(!(ctx->seed_rnd = ub_initstate(seed, NULL))) { if(!(ctx->seed_rnd = ub_initstate(seed, NULL))) {
seed = 0; explicit_bzero(&seed, sizeof(seed));
ub_randfree(ctx->seed_rnd); ub_randfree(ctx->seed_rnd);
free(ctx); free(ctx);
errno = ENOMEM; errno = ENOMEM;
return NULL; return NULL;
} }
seed = 0; explicit_bzero(&seed, sizeof(seed));
lock_basic_init(&ctx->qqpipe_lock); lock_basic_init(&ctx->qqpipe_lock);
lock_basic_init(&ctx->rrpipe_lock); lock_basic_init(&ctx->rrpipe_lock);
lock_basic_init(&ctx->cfglock); lock_basic_init(&ctx->cfglock);
@ -392,7 +392,6 @@ ub_ctx_add_ta(struct ub_ctx* ctx, const char* ta)
} }
if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_list, dup)) { if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_list, dup)) {
lock_basic_unlock(&ctx->cfglock); lock_basic_unlock(&ctx->cfglock);
free(dup);
return UB_NOMEM; return UB_NOMEM;
} }
lock_basic_unlock(&ctx->cfglock); lock_basic_unlock(&ctx->cfglock);
@ -412,7 +411,6 @@ ub_ctx_add_ta_file(struct ub_ctx* ctx, const char* fname)
} }
if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_file_list, dup)) { if(!cfg_strlist_insert(&ctx->env->cfg->trust_anchor_file_list, dup)) {
lock_basic_unlock(&ctx->cfglock); lock_basic_unlock(&ctx->cfglock);
free(dup);
return UB_NOMEM; return UB_NOMEM;
} }
lock_basic_unlock(&ctx->cfglock); lock_basic_unlock(&ctx->cfglock);
@ -432,7 +430,6 @@ int ub_ctx_add_ta_autr(struct ub_ctx* ctx, const char* fname)
if(!cfg_strlist_insert(&ctx->env->cfg->auto_trust_anchor_file_list, if(!cfg_strlist_insert(&ctx->env->cfg->auto_trust_anchor_file_list,
dup)) { dup)) {
lock_basic_unlock(&ctx->cfglock); lock_basic_unlock(&ctx->cfglock);
free(dup);
return UB_NOMEM; return UB_NOMEM;
} }
lock_basic_unlock(&ctx->cfglock); lock_basic_unlock(&ctx->cfglock);
@ -452,7 +449,6 @@ ub_ctx_trustedkeys(struct ub_ctx* ctx, const char* fname)
} }
if(!cfg_strlist_insert(&ctx->env->cfg->trusted_keys_file_list, dup)) { if(!cfg_strlist_insert(&ctx->env->cfg->trusted_keys_file_list, dup)) {
lock_basic_unlock(&ctx->cfglock); lock_basic_unlock(&ctx->cfglock);
free(dup);
return UB_NOMEM; return UB_NOMEM;
} }
lock_basic_unlock(&ctx->cfglock); lock_basic_unlock(&ctx->cfglock);
@ -962,7 +958,6 @@ ub_ctx_set_fwd(struct ub_ctx* ctx, const char* addr)
return UB_NOMEM; return UB_NOMEM;
} }
if(!cfg_strlist_insert(&s->addrs, dupl)) { if(!cfg_strlist_insert(&s->addrs, dupl)) {
free(dupl);
lock_basic_unlock(&ctx->cfglock); lock_basic_unlock(&ctx->cfglock);
errno=ENOMEM; errno=ENOMEM;
return UB_NOMEM; return UB_NOMEM;
@ -1045,7 +1040,6 @@ int ub_ctx_set_stub(struct ub_ctx* ctx, const char* zone, const char* addr,
} }
if(!cfg_strlist_insert(&elem->addrs, a)) { if(!cfg_strlist_insert(&elem->addrs, a)) {
lock_basic_unlock(&ctx->cfglock); lock_basic_unlock(&ctx->cfglock);
free(a);
errno = ENOMEM; errno = ENOMEM;
return UB_NOMEM; return UB_NOMEM;
} }
@ -1233,7 +1227,6 @@ ub_ctx_hosts(struct ub_ctx* ctx, const char* fname)
ins)) { ins)) {
lock_basic_unlock(&ctx->cfglock); lock_basic_unlock(&ctx->cfglock);
fclose(in); fclose(in);
free(ins);
errno=ENOMEM; errno=ENOMEM;
return UB_NOMEM; return UB_NOMEM;
} }

View file

@ -187,7 +187,7 @@ libworker_setup(struct ub_ctx* ctx, int is_bg, struct ub_event_base* eb)
if(!w->is_bg || w->is_bg_thread) { if(!w->is_bg || w->is_bg_thread) {
lock_basic_unlock(&ctx->cfglock); lock_basic_unlock(&ctx->cfglock);
} }
seed = 0; explicit_bzero(&seed, sizeof(seed));
libworker_delete(w); libworker_delete(w);
return NULL; return NULL;
} }
@ -207,7 +207,7 @@ libworker_setup(struct ub_ctx* ctx, int is_bg, struct ub_event_base* eb)
hash_set_raninit((uint32_t)ub_random(w->env->rnd)); hash_set_raninit((uint32_t)ub_random(w->env->rnd));
} }
} }
seed = 0; explicit_bzero(&seed, sizeof(seed));
if(eb) if(eb)
w->base = comm_base_create_event(eb); w->base = comm_base_create_event(eb);

View file

@ -2349,7 +2349,7 @@ int main(int argc, char* argv[])
} }
} }
argc -= optind; argc -= optind;
argv += optind; /* argv += optind; not using further arguments */
if(argc != 0) if(argc != 0)
usage(); usage();

View file

@ -788,7 +788,7 @@ service_tcp_relay(struct tcp_proxy** tcp_proxies, struct timeval* now,
if(!tcp_relay_write(p->server_s, &p->querylist, if(!tcp_relay_write(p->server_s, &p->querylist,
&p->querylast, now)) &p->querylast, now))
delete_it = 1; delete_it = 1;
if(p->querylist && p->server_s != -1 && if(p->querylist &&
dl_tv_smaller(&p->querylist->wait, now)) dl_tv_smaller(&p->querylist->wait, now))
FD_SET(FD_SET_T p->server_s, worig); FD_SET(FD_SET_T p->server_s, worig);
else FD_CLR(FD_SET_T p->server_s, worig); else FD_CLR(FD_SET_T p->server_s, worig);

View file

@ -417,7 +417,7 @@ provide_file_10(SSL* ssl, char* fname)
} }
fclose(in); fclose(in);
at += len; at += len;
avail -= len; /* avail -= len; unused */
if(SSL_write(ssl, buf, at-buf) <= 0) { if(SSL_write(ssl, buf, at-buf) <= 0) {
/* write failure */ /* write failure */
} }
@ -506,7 +506,7 @@ provide_file_chunked(SSL* ssl, char* fname)
snprintf(at, avail, "\r\n"); snprintf(at, avail, "\r\n");
r = strlen(at); r = strlen(at);
at += r; at += r;
avail -= r; /* avail -= r; unused */
} }
/* send chunk */ /* send chunk */
if(SSL_write(ssl, buf, at-buf) <= 0) { if(SSL_write(ssl, buf, at-buf) <= 0) {
@ -569,7 +569,9 @@ do_service(char* addr, int port, char* key, char* cert)
while(go) { while(go) {
struct sockaddr_storage from; struct sockaddr_storage from;
socklen_t flen = (socklen_t)sizeof(from); socklen_t flen = (socklen_t)sizeof(from);
int s = accept(fd, (struct sockaddr*)&from, &flen); int s;
memset(&from, 0, sizeof(from));
s = accept(fd, (struct sockaddr*)&from, &flen);
if(verb) fflush(stdout); if(verb) fflush(stdout);
if(s != -1) { if(s != -1) {
SSL* ssl = setup_ssl(s, sslctx); SSL* ssl = setup_ssl(s, sslctx);
@ -633,7 +635,7 @@ int main(int argc, char* argv[])
} }
} }
argc -= optind; argc -= optind;
argv += optind; /* argv += optind; not using further arguments */
if(argc != 0) if(argc != 0)
usage(); usage();

View file

@ -429,14 +429,14 @@ main(int argc, char* argv[])
case 'h': case 'h':
default: default:
testbound_usage(); testbound_usage();
return 1; exit(1);
} }
} }
argc -= optind; argc -= optind;
argv += optind; /* argv += optind; not using further arguments */
if(argc != 0) { if(argc != 0) {
testbound_usage(); testbound_usage();
return 1; exit(1);
} }
log_info("Start of %s testbound program.", PACKAGE_STRING); log_info("Start of %s testbound program.", PACKAGE_STRING);
if(atexit(&remove_configfile) != 0) if(atexit(&remove_configfile) != 0)

View file

@ -46,6 +46,7 @@ enum verbosity_value { NO_VERBOSE=0 };
#endif #endif
/** logging routine, provided by caller */ /** logging routine, provided by caller */
void verbose(enum verbosity_value lvl, const char* msg, ...) ATTR_FORMAT(printf, 2, 3); void verbose(enum verbosity_value lvl, const char* msg, ...) ATTR_FORMAT(printf, 2, 3);
static void error(const char* msg, ...) ATTR_NORETURN;
/** print error and exit */ /** print error and exit */
static void error(const char* msg, ...) static void error(const char* msg, ...)

View file

@ -1578,11 +1578,15 @@ int
cfg_strlist_insert(struct config_strlist** head, char* item) cfg_strlist_insert(struct config_strlist** head, char* item)
{ {
struct config_strlist *s; struct config_strlist *s;
if(!item || !head) if(!item || !head) {
free(item);
return 0; return 0;
}
s = (struct config_strlist*)calloc(1, sizeof(struct config_strlist)); s = (struct config_strlist*)calloc(1, sizeof(struct config_strlist));
if(!s) if(!s) {
free(item);
return 0; return 0;
}
s->str = item; s->str = item;
s->next = *head; s->next = *head;
*head = s; *head = s;
@ -1593,11 +1597,17 @@ int
cfg_str2list_insert(struct config_str2list** head, char* item, char* i2) cfg_str2list_insert(struct config_str2list** head, char* item, char* i2)
{ {
struct config_str2list *s; struct config_str2list *s;
if(!item || !i2 || !head) if(!item || !i2 || !head) {
free(item);
free(i2);
return 0; return 0;
}
s = (struct config_str2list*)calloc(1, sizeof(struct config_str2list)); s = (struct config_str2list*)calloc(1, sizeof(struct config_str2list));
if(!s) if(!s) {
free(item);
free(i2);
return 0; return 0;
}
s->str = item; s->str = item;
s->str2 = i2; s->str2 = i2;
s->next = *head; s->next = *head;

View file

@ -809,6 +809,7 @@ struct config_strlist* cfg_strlist_find(struct config_strlist* head,
* @param head: pointer to strlist head variable. * @param head: pointer to strlist head variable.
* @param item: new item. malloced by caller. If NULL the insertion fails. * @param item: new item. malloced by caller. If NULL the insertion fails.
* @return: true on success. * @return: true on success.
* on fail, the item is free()d.
*/ */
int cfg_strlist_insert(struct config_strlist** head, char* item); int cfg_strlist_insert(struct config_strlist** head, char* item);
@ -822,6 +823,7 @@ int cfg_region_strlist_insert(struct regional* region,
* @param item: new item. malloced by caller. If NULL the insertion fails. * @param item: new item. malloced by caller. If NULL the insertion fails.
* @param i2: 2nd string, malloced by caller. If NULL the insertion fails. * @param i2: 2nd string, malloced by caller. If NULL the insertion fails.
* @return: true on success. * @return: true on success.
* on fail, the item and i2 are free()d.
*/ */
int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2); int cfg_str2list_insert(struct config_str2list** head, char* item, char* i2);

View file

@ -174,7 +174,7 @@ void log_buf(enum verbosity_value level, const char* msg, struct sldns_buffer* b
* Pass printf formatted arguments. No trailing newline is needed. * Pass printf formatted arguments. No trailing newline is needed.
* @param format: printf-style format string. Arguments follow. * @param format: printf-style format string. Arguments follow.
*/ */
void fatal_exit(const char* format, ...) ATTR_FORMAT(printf, 1, 2); void fatal_exit(const char* format, ...) ATTR_FORMAT(printf, 1, 2) ATTR_NORETURN;
/** /**
* va_list argument version of log_info. * va_list argument version of log_info.