Improve comments and defensive programming for 3045

The comment fixes are trivial.  The defensive programming trick is to
tolerate receiving NULL inputs on the describe functions. That should
never actually happen, but it seems like the likeliest mistake for us
to make in the future.
This commit is contained in:
Nick Mathewson 2011-05-30 15:15:10 -04:00
parent b6eee716a8
commit 3c0d944b07
2 changed files with 18 additions and 2 deletions

View File

@ -2223,8 +2223,7 @@ is_legal_hexdigest(const char *s)
* and address <b>addr</b> or <b>addr32h</b>. * and address <b>addr</b> or <b>addr32h</b>.
* *
* The <b>nickname</b> and <b>addr</b> fields are optional and may be set to * The <b>nickname</b> and <b>addr</b> fields are optional and may be set to
* NULL. The <b>addr32h</b> field is optional and may be set to * NULL. The <b>addr32h</b> field is optional and may be set to 0.
* <b>addr32h</b>.
* *
* Return a pointer to the front of <b>buf</b>. * Return a pointer to the front of <b>buf</b>.
*/ */
@ -2237,6 +2236,10 @@ format_node_description(char *buf,
uint32_t addr32h) uint32_t addr32h)
{ {
char *cp; char *cp;
if (!buf)
return "<NULL BUFFER>";
buf[0] = '$'; buf[0] = '$';
base16_encode(buf+1, HEX_DIGEST_LEN+1, id_digest, DIGEST_LEN); base16_encode(buf+1, HEX_DIGEST_LEN+1, id_digest, DIGEST_LEN);
cp = buf+1+HEX_DIGEST_LEN; cp = buf+1+HEX_DIGEST_LEN;
@ -2268,6 +2271,8 @@ format_node_description(char *buf,
const char * const char *
router_get_description(char *buf, const routerinfo_t *ri) router_get_description(char *buf, const routerinfo_t *ri)
{ {
if (!ri)
return "<null>";
return format_node_description(buf, return format_node_description(buf,
ri->cache_info.identity_digest, ri->cache_info.identity_digest,
ri->is_named, ri->is_named,
@ -2284,6 +2289,8 @@ router_get_description(char *buf, const routerinfo_t *ri)
const char * const char *
routerstatus_get_description(char *buf, const routerstatus_t *rs) routerstatus_get_description(char *buf, const routerstatus_t *rs)
{ {
if (!rs)
return "<null>";
return format_node_description(buf, return format_node_description(buf,
rs->identity_digest, rs->identity_digest,
rs->is_named, rs->is_named,
@ -2300,6 +2307,8 @@ routerstatus_get_description(char *buf, const routerstatus_t *rs)
const char * const char *
extend_info_get_description(char *buf, const extend_info_t *ei) extend_info_get_description(char *buf, const extend_info_t *ei)
{ {
if (!ei)
return "<null>";
return format_node_description(buf, return format_node_description(buf,
ei->identity_digest, ei->identity_digest,
0, 0,

View File

@ -86,6 +86,13 @@ int is_legal_nickname(const char *s);
int is_legal_nickname_or_hexdigest(const char *s); int is_legal_nickname_or_hexdigest(const char *s);
int is_legal_hexdigest(const char *s); int is_legal_hexdigest(const char *s);
/**
* Longest allowed output of format_node_description, plus 1 character for
* NUL. This allows space for:
* "$FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF~xxxxxxxxxxxxxxxxxxx at"
* " [ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]"
* plus a terminating NUL.
*/
#define NODE_DESC_BUF_LEN (MAX_VERBOSE_NICKNAME_LEN+4+TOR_ADDR_BUF_LEN) #define NODE_DESC_BUF_LEN (MAX_VERBOSE_NICKNAME_LEN+4+TOR_ADDR_BUF_LEN)
const char *format_node_description(char *buf, const char *format_node_description(char *buf,
const char *id_digest, const char *id_digest,