Refactor out signed/unsigned comparisons.

This commit is contained in:
Jason Evans 2016-03-15 09:35:14 -07:00
parent 434ea64b26
commit 22af74e106
5 changed files with 14 additions and 18 deletions

View File

@ -106,9 +106,9 @@ void malloc_write(const char *s);
* malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating * malloc_vsnprintf() supports a subset of snprintf(3) that avoids floating
* point math. * point math.
*/ */
int malloc_vsnprintf(char *str, size_t size, const char *format, size_t malloc_vsnprintf(char *str, size_t size, const char *format,
va_list ap); va_list ap);
int malloc_snprintf(char *str, size_t size, const char *format, ...) size_t malloc_snprintf(char *str, size_t size, const char *format, ...)
JEMALLOC_FORMAT_PRINTF(3, 4); JEMALLOC_FORMAT_PRINTF(3, 4);
void malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque, void malloc_vcprintf(void (*write_cb)(void *, const char *), void *cbopaque,
const char *format, va_list ap); const char *format, va_list ap);

View File

@ -314,10 +314,9 @@ x2s(uintmax_t x, bool alt_form, bool uppercase, char *s, size_t *slen_p)
return (s); return (s);
} }
int size_t
malloc_vsnprintf(char *str, size_t size, const char *format, va_list ap) malloc_vsnprintf(char *str, size_t size, const char *format, va_list ap)
{ {
int ret;
size_t i; size_t i;
const char *f; const char *f;
@ -585,21 +584,19 @@ malloc_vsnprintf(char *str, size_t size, const char *format, va_list ap)
str[i] = '\0'; str[i] = '\0';
else else
str[size - 1] = '\0'; str[size - 1] = '\0';
assert(i < INT_MAX);
ret = (int)i;
#undef APPEND_C #undef APPEND_C
#undef APPEND_S #undef APPEND_S
#undef APPEND_PADDED_S #undef APPEND_PADDED_S
#undef GET_ARG_NUMERIC #undef GET_ARG_NUMERIC
return (ret); return (i);
} }
JEMALLOC_FORMAT_PRINTF(3, 4) JEMALLOC_FORMAT_PRINTF(3, 4)
int size_t
malloc_snprintf(char *str, size_t size, const char *format, ...) malloc_snprintf(char *str, size_t size, const char *format, ...)
{ {
int ret; size_t ret;
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);

View File

@ -32,9 +32,8 @@ timer_ratio(timedelta_t *a, timedelta_t *b, char *buf, size_t buflen)
uint64_t t0 = timer_usec(a); uint64_t t0 = timer_usec(a);
uint64_t t1 = timer_usec(b); uint64_t t1 = timer_usec(b);
uint64_t mult; uint64_t mult;
unsigned i = 0; size_t i = 0;
unsigned j; size_t j, n;
int n;
/* Whole. */ /* Whole. */
n = malloc_snprintf(&buf[i], buflen-i, "%"FMTu64, t0 / t1); n = malloc_snprintf(&buf[i], buflen-i, "%"FMTu64, t0 / t1);

View File

@ -101,7 +101,7 @@ TEST_BEGIN(test_bitmap_sfu)
bitmap_info_t binfo; bitmap_info_t binfo;
bitmap_info_init(&binfo, i); bitmap_info_init(&binfo, i);
{ {
ssize_t j; size_t j;
bitmap_t *bitmap = (bitmap_t *)malloc( bitmap_t *bitmap = (bitmap_t *)malloc(
bitmap_size(&binfo)); bitmap_size(&binfo));
bitmap_init(bitmap, &binfo); bitmap_init(bitmap, &binfo);
@ -119,7 +119,7 @@ TEST_BEGIN(test_bitmap_sfu)
* Iteratively unset bits starting at the end, and * Iteratively unset bits starting at the end, and
* verify that bitmap_sfu() reaches the unset bits. * verify that bitmap_sfu() reaches the unset bits.
*/ */
for (j = i - 1; j >= 0; j--) { for (j = i - 1; j < i; j--) { /* (i..0] */
bitmap_unset(bitmap, &binfo, j); bitmap_unset(bitmap, &binfo, j);
assert_zd_eq(bitmap_sfu(bitmap, &binfo), j, assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
"First unset bit should the bit previously " "First unset bit should the bit previously "

View File

@ -160,14 +160,14 @@ TEST_BEGIN(test_malloc_snprintf_truncated)
{ {
#define BUFLEN 15 #define BUFLEN 15
char buf[BUFLEN]; char buf[BUFLEN];
int result; size_t result;
size_t len; size_t len;
#define TEST(expected_str_untruncated, ...) do { \ #define TEST(expected_str_untruncated, ...) do { \
result = malloc_snprintf(buf, len, __VA_ARGS__); \ result = malloc_snprintf(buf, len, __VA_ARGS__); \
assert_d_eq(strncmp(buf, expected_str_untruncated, len-1), 0, \ assert_d_eq(strncmp(buf, expected_str_untruncated, len-1), 0, \
"Unexpected string inequality (\"%s\" vs \"%s\")", \ "Unexpected string inequality (\"%s\" vs \"%s\")", \
buf, expected_str_untruncated); \ buf, expected_str_untruncated); \
assert_d_eq(result, strlen(expected_str_untruncated), \ assert_zu_eq(result, strlen(expected_str_untruncated), \
"Unexpected result"); \ "Unexpected result"); \
} while (0) } while (0)
@ -193,11 +193,11 @@ TEST_BEGIN(test_malloc_snprintf)
{ {
#define BUFLEN 128 #define BUFLEN 128
char buf[BUFLEN]; char buf[BUFLEN];
int result; size_t result;
#define TEST(expected_str, ...) do { \ #define TEST(expected_str, ...) do { \
result = malloc_snprintf(buf, sizeof(buf), __VA_ARGS__); \ result = malloc_snprintf(buf, sizeof(buf), __VA_ARGS__); \
assert_str_eq(buf, expected_str, "Unexpected output"); \ assert_str_eq(buf, expected_str, "Unexpected output"); \
assert_d_eq(result, strlen(expected_str), "Unexpected result"); \ assert_zu_eq(result, strlen(expected_str), "Unexpected result");\
} while (0) } while (0)
TEST("hello", "hello"); TEST("hello", "hello");