From 2b44fb38172ff0d64d1d5d907e3322d5e1a6a8ca Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 25 May 2020 12:55:33 +0100 Subject: [PATCH] adns_str* etc.: Return NULL rather than crashing, and docs Previously, calling these functions with arbitrary values would cause them to do a struct member lookup on a null pointer. Now they return the pointer instead. Also, some users have been confused by the docs, and have passsed, for example, adns_s_max_misconfig. This is also not permitted. Clarify that only values actually returned by adns are allowed. Signed-off-by: Ian Jackson --- src/adns.h | 7 +++++-- src/general.c | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/adns.h b/src/adns.h index 899a731..b8072ee 100644 --- a/src/adns.h +++ b/src/adns.h @@ -1085,8 +1085,11 @@ const char *adns_errtypeabbrev(adns_status st); * the abbreviation of the error - eg, for adns_s_timeout it returns * "timeout". adns_errtypeabbrev returns the abbreviation of the * error class: ie, for values up to adns_s_max_XXX it will return the - * string XXX. You MUST NOT call these functions with status values - * not returned by the same adns library. + * string XXX. + * + * If you call these functions with status values not actually + * returned from other functions in the same adns library, the + * returned information may be NULL. */ #ifdef __cplusplus diff --git a/src/general.c b/src/general.c index e2ab3bc..cfe07bd 100644 --- a/src/general.c +++ b/src/general.c @@ -288,14 +288,14 @@ const char *adns_strerror(adns_status st) { const struct sinfo *si; si= findsinfo(st); - return si->string; + return si ? si->string : 0; } const char *adns_errabbrev(adns_status st) { const struct sinfo *si; si= findsinfo(st); - return si->abbrev; + return si ? si->abbrev : 0; } @@ -332,7 +332,7 @@ const char *adns_errtypeabbrev(adns_status st) { sti= bsearch(&st,stinfos, sizeof(stinfos)/sizeof(*stinfos), sizeof(*stinfos), sti_compar); - return sti->abbrev; + return sti ? sti->abbrev : 0; } -- 2.30.2