static PREFIX_STATS *stats_prefix_find(const char *key, const size_t nkey) { PREFIX_STATS *pfs; uint32_t hashval; size_t length; bool bailout = true; // [branch 008] Switch to safe assertions tm_assert(key != NULL); for (length = 0; length < nkey && key[length] != '\0'; length++) { if (key[length] == settings.prefix_delimiter) { bailout = false; break; } } if (bailout) { return NULL; } hashval = hash(key, length, 0) % PREFIX_HASH_SIZE; for (pfs = prefix_stats[hashval]; NULL != pfs; pfs = pfs->next) { // [branch 009] Switch to tm-safe strncmp if (tm_strncmp(pfs->prefix, key, length) == 0) return pfs; } pfs = calloc(sizeof(PREFIX_STATS), 1); if (NULL == pfs) { // [branch 012] move perror to oncommit registerOnCommitHandler(spf_perror1, (void*)(uintptr_t)errno); return NULL; } pfs->prefix = malloc(length + 1); if (NULL == pfs->prefix) { // [branch 012] move perror to oncommit registerOnCommitHandler(spf_perror2, (void*)(uintptr_t)errno); free(pfs); return NULL; } // [branch 009] Switch to safe strncpy tm_strncpy(pfs->prefix, key, length); pfs->prefix[length] = '\0'; /* because strncpy() sucks */ pfs->prefix_len = length; pfs->next = prefix_stats[hashval]; prefix_stats[hashval] = pfs; num_prefixes++; total_prefix_size += length; return pfs; }
/* get next word to sum up */ void next_word(str_char **buf, unsigned int *word, int *done) { ASSERT(buf); ASSERT(*buf); ASSERT(word); ASSERT(done); *done = FALSE; /* put a word full of characters into variable "word" */ tm_strncpy((str_char*) word, *buf, sizeof(unsigned int)); /* If a zero was appended to word, we are done since we saw the whole input string. */ if(((str_char*) word)[sizeof(unsigned int) - 1] == _TEXT('\0')) *done = TRUE; *buf += sizeof(unsigned int)/sizeof(str_char); }
void tm_string_copy_to_chars(str_char *dest_chars, tm_string *source_string, unsigned int sz_dest_chars, int *truncated) { ASSERT(dest_chars); ASSERT(sz_dest_chars >= 1); ASSERT(source_string); ASSERT(truncated); tm_strncpy(dest_chars, source_string->contents, sz_dest_chars); /* Make sure that the output is null terminated, just in case tm_strncpy did not put in a null character because it truncated the output. */ dest_chars[sz_dest_chars - 1] = _TEXT('\0'); /* Determine if output was truncated. */ if (source_string->length + 1 > sz_dest_chars) *truncated = TRUE; else *truncated = FALSE; }