diff options
author | Bob Beck <beck@cvs.openbsd.org> | 2014-06-01 22:42:46 +0000 |
---|---|---|
committer | Bob Beck <beck@cvs.openbsd.org> | 2014-06-01 22:42:46 +0000 |
commit | de2df71f5ba61f19759640a70af2003091b306af (patch) | |
tree | 0da09634fca81fae131d9f98eab8fc990c0906a1 /lib | |
parent | 86645e8cac011c676064caad15721309014622f0 (diff) |
Clean up some of the nightmare of string and pointer arithmatic in
this nasty function.
This gets rid of the nasty tmp variables used to hold temporary strings
and the DECIMAL_SIZE hack. it gets rid of the rather pointless null checks
for buf (since the original code dereferences it before checking). It also
gets rid of the insane possibility this could return -1 when stuff is
using the return values to compute lengths All the failure cases now
return 0 and an empty string like the first error case in the original
code.
ok miod@ tedu@
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libcrypto/objects/obj_dat.c | 88 |
1 files changed, 36 insertions, 52 deletions
diff --git a/lib/libcrypto/objects/obj_dat.c b/lib/libcrypto/objects/obj_dat.c index a71959222ff..301c8a266b9 100644 --- a/lib/libcrypto/objects/obj_dat.c +++ b/lib/libcrypto/objects/obj_dat.c @@ -485,16 +485,14 @@ OBJ_txt2obj(const char *s, int no_name) int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name) { - int i, n = 0, len, nid, first, use_bn; - BIGNUM *bl; + int i, ret = 0, len, nid, first = 1, use_bn; + BIGNUM *bl = NULL; + char *bndec = NULL; unsigned long l; const unsigned char *p; - char tbuf[DECIMAL_SIZE(l) + 1]; - if ((a == NULL) || (a->data == NULL)) { - buf[0] = '\0'; - return (0); - } + if ((a == NULL) || (a->data == NULL)) + goto err; if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) { const char *s; @@ -502,19 +500,14 @@ OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name) if (s == NULL) s = OBJ_nid2sn(nid); if (s) { - if (buf) - strlcpy(buf, s, buf_len); - n = strlen(s); - return n; + ret = strlcpy(buf, s, buf_len); + goto out; } } len = a->length; p = a->data; - first = 1; - bl = NULL; - while (len > 0) { l = 0; use_bn = 0; @@ -557,62 +550,53 @@ OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name) i = (int)(l / 40); l -= (long)(i * 40); } - if (buf && (buf_len > 0)) { + if (buf_len > 0) { *buf++ = i + '0'; buf_len--; } - n++; + ret++; } if (use_bn) { - char *bndec; bndec = BN_bn2dec(bl); if (!bndec) goto err; - i = strlen(bndec); - if (buf) { - if (buf_len > 0) { - *buf++ = '.'; - buf_len--; - } - strlcpy(buf, bndec, buf_len); - if (i > buf_len) { - buf += buf_len; - buf_len = 0; - } else { - buf += i; - buf_len -= i; - } + i = snprintf(buf, buf_len, ".%s", bndec); + if (i == -1) + goto err; + if (i >= buf_len) { + buf += buf_len; + buf_len = 0; + } else { + buf += i; + buf_len -= i; } - n++; - n += i; - free(bndec); + ret += i; } else { - (void) snprintf(tbuf, sizeof tbuf, ".%lu", l); - i = strlen(tbuf); - if (buf && (buf_len > 0)) { - strlcpy(buf, tbuf, buf_len); - if (i > buf_len) { - buf += buf_len; - buf_len = 0; - } else { - buf += i; - buf_len -= i; - } + i = snprintf(buf, buf_len, ".%lu", l); + if (i == -1) + goto err; + if (i >= buf_len) { + buf += buf_len; + buf_len = 0; + } else { + buf += i; + buf_len -= i; } - n += i; + ret += i; l = 0; } } - if (bl) - BN_free(bl); - return n; +out: + free(bndec); + BN_free(bl); + return ret; err: - if (bl) - BN_free(bl); - return -1; + ret = 0; + buf[0] = '\0'; + goto out; } int |