diff options
| author | christos <christos@NetBSD.org> | 2016-03-07 14:35:39 +0000 |
|---|---|---|
| committer | christos <christos@NetBSD.org> | 2016-03-07 14:35:39 +0000 |
| commit | 7163a5677bf68a4e46752bb005ce676181850174 (patch) | |
| tree | 2ffa3c71c90acdb61dc41ad18d627260856f9916 /lib/libresolv | |
| parent | 192fa6599e0a9b834030ed060457a7d692d073f7 (diff) | |
PR/50907: David Binderman: Remove useless strlen()'s and memcpy()'s.
Diffstat (limited to 'lib/libresolv')
| -rw-r--r-- | lib/libresolv/hmac_link.c | 66 |
1 files changed, 33 insertions, 33 deletions
diff --git a/lib/libresolv/hmac_link.c b/lib/libresolv/hmac_link.c index be9079ac7a3..f63823dc42e 100644 --- a/lib/libresolv/hmac_link.c +++ b/lib/libresolv/hmac_link.c @@ -1,4 +1,4 @@ -/* $NetBSD: hmac_link.c,v 1.2 2012/11/16 02:16:38 christos Exp $ */ +/* $NetBSD: hmac_link.c,v 1.3 2016/03/07 14:35:39 christos Exp $ */ /* * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc. @@ -20,7 +20,7 @@ #if 0 static const char rcsid[] = "Header: /proj/cvs/prod/libbind/dst/hmac_link.c,v 1.8 2007/09/24 17:18:25 each Exp "; #else -__RCSID("$NetBSD: hmac_link.c,v 1.2 2012/11/16 02:16:38 christos Exp $"); +__RCSID("$NetBSD: hmac_link.c,v 1.3 2016/03/07 14:35:39 christos Exp $"); #endif /*% @@ -268,48 +268,48 @@ dst_hmac_md5_key_to_file_format(const DST_KEY *dkey, char *buff, const int buff_len) { char *bp; - int len, i, key_len; +#define BUF_LEFT (size_t)(buff_len - (bp - buff)) + int len, key_len; u_char key[HMAC_LEN]; HMAC_Key *hkey; + static const char keystr[] = "Key: "; + + if (buff == NULL) + return -1; /*%< no output area */ if (dkey == NULL || dkey->dk_KEY_struct == NULL) - return (0); - /* - * Using snprintf() would be so much simpler here. - */ - if (buff == NULL || - buff_len <= (int)(strlen(KEY_FILE_FMT_STR) + - strlen(KEY_FILE_FORMAT) + 4)) - return (-1); /*%< no OR not enough space in output area */ - hkey = (HMAC_Key *) dkey->dk_KEY_struct; - memset(buff, 0, buff_len); /*%< just in case */ + return 0; + /* write file header */ - snprintf(buff, buff_len, KEY_FILE_FMT_STR, KEY_FILE_FORMAT, + hkey = (HMAC_Key *) dkey->dk_KEY_struct; + len = snprintf(buff, buff_len, KEY_FILE_FMT_STR, KEY_FILE_FORMAT, KEY_HMAC_MD5, "HMAC"); - - bp = buff + strlen(buff); - - memset(key, 0, HMAC_LEN); - for (i = 0; i < HMAC_LEN; i++) - key[i] = hkey->hk_ipad[i] ^ HMAC_IPAD; - for (i = HMAC_LEN - 1; i >= 0; i--) - if (key[i] != 0) + if (len < 0 || len >= buff_len) + return -1; /*%< not enough space in output area */ + bp = buff + len; + if (BUF_LEFT < sizeof(keystr)) + return -1; + + memcpy(bp, keystr, sizeof(keystr) - 1); + bp += sizeof(keystr) - 1; + + for (key_len = 0; key_len < HMAC_LEN; key_len++) + key[key_len] = hkey->hk_ipad[key_len] ^ HMAC_IPAD; + for (key_len = HMAC_LEN - 1; key_len >= 0; key_len--) + if (key[key_len] != 0) break; - key_len = i + 1; + key_len++; - if (buff_len - (bp - buff) < 6) - return (-1); - strcat(bp, "Key: "); - bp += strlen("Key: "); - - len = b64_ntop(key, key_len, bp, (size_t)(buff_len - (bp - buff))); + len = b64_ntop(key, key_len, bp, BUF_LEFT); if (len < 0) - return (-1); + return -1; bp += len; - if (buff_len - (bp - buff) < 2) - return (-1); + + if (BUF_LEFT < 2) + return -1; *(bp++) = '\n'; - *bp = '\0'; + + memset(bp, 0, BUF_LEFT); return (int)(bp - buff); } |
