Fix off-by-one buffer size errors when using tohex()

This commit is contained in:
Andrew Bettison 2013-11-15 17:10:33 +10:30
parent f2b652c094
commit b8e0859880
3 changed files with 8 additions and 7 deletions

View File

@ -740,10 +740,10 @@ int _sqlite_vbind(struct __sourceloc __whence, int log_level, sqlite_retry_state
if (hashp == NULL) {
BIND_NULL(RHIZOME_FILEHASH_T);
} else {
char hash_hex[RHIZOME_FILEHASH_STRLEN];
tohex(hash_hex, sizeof hash_hex, hashp->binary);
BIND_DEBUG(RHIZOME_FILEHASH_T, sqlite3_bind_text, "%s,%zu,SQLITE_TRANSIENT", hash_hex, sizeof hash_hex);
BIND_RETRY(sqlite3_bind_text, hash_hex, sizeof hash_hex, SQLITE_TRANSIENT);
char hash_hex[RHIZOME_FILEHASH_STRLEN + 1];
tohex(hash_hex, RHIZOME_FILEHASH_STRLEN, hashp->binary);
BIND_DEBUG(RHIZOME_FILEHASH_T, sqlite3_bind_text, "%s,%zu,SQLITE_TRANSIENT", hash_hex, RHIZOME_FILEHASH_STRLEN);
BIND_RETRY(sqlite3_bind_text, hash_hex, RHIZOME_FILEHASH_STRLEN, SQLITE_TRANSIENT);
}
}
break;
@ -1961,7 +1961,7 @@ static int is_interesting(const char *id_hex, int64_t version)
int rhizome_is_bar_interesting(unsigned char *bar)
{
int64_t version = rhizome_bar_version(bar);
char id_hex[RHIZOME_MANIFEST_ID_STRLEN];
char id_hex[RHIZOME_BAR_PREFIX_BYTES + 2];
tohex(id_hex, RHIZOME_BAR_PREFIX_BYTES * 2, &bar[RHIZOME_BAR_PREFIX_OFFSET]);
strcat(id_hex, "%");
return is_interesting(id_hex, version);

View File

@ -157,7 +157,7 @@ typedef struct sid_binary {
#define is_sid_t_any(SID) is_all_matching((SID).binary, sizeof (*(sid_t*)0).binary, 0)
#define alloca_tohex_sid_t(sid) alloca_tohex((sid).binary, sizeof (*(sid_t*)0).binary)
#define alloca_tohex_sid_t_trunc(sid,strlen) tohex((char *)alloca((strlen)+2), (strlen), (sid).binary)
#define alloca_tohex_sid_t_trunc(sid,strlen) tohex((char *)alloca((strlen)+1), (strlen), (sid).binary)
int cmp_sid_t(const sid_t *a, const sid_t *b);
int str_to_sid_t(sid_t *sid, const char *hex);

3
str.h
View File

@ -74,7 +74,8 @@ __SERVAL_DNA_STR_INLINE int is_xstring(const char *text, int len)
return *text == '\0';
}
/* Converts a given binary blob to uppercase ASCII hexadecimal.
/* Converts a given binary blob to uppercase ASCII hexadecimal with a NUL terminator on the end.
* 'dstHex' must point to a buffer of at least 'dstStrLen' + 1 bytes.
*/
char *tohex(char *dstHex, size_t dstStrlen, const unsigned char *srcBinary);
#define alloca_tohex(buf,bytes) tohex((char *)alloca((bytes)*2+1), (bytes) * 2, (buf))