mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2026-09-29 13:37:24 +00:00
Test printf integer format modifiers more
In particular, test `"%zu"` for `size_t` and `"%lld"` for `long long`, which older Windows runtimes do not support, but which Mbed TLS uses. Signed-off-by: Gilles Peskine <[email protected]>
This commit is contained in:
@@ -3,58 +3,83 @@
|
|||||||
# and strings through the test framework.
|
# and strings through the test framework.
|
||||||
|
|
||||||
printf "%d", 0
|
printf "%d", 0
|
||||||
printf_int:"%d":0:"0"
|
printf_integer:"%d":PRINTF_CAST_INT:0:"0"
|
||||||
|
|
||||||
printf "%d", -0
|
printf "%d", -0
|
||||||
printf_int:"%d":-0:"0"
|
printf_integer:"%d":PRINTF_CAST_INT:-0:"0"
|
||||||
|
|
||||||
printf "%d", 0x0
|
printf "%d", 0x0
|
||||||
printf_int:"%d":0x0:"0"
|
printf_integer:"%d":PRINTF_CAST_INT:0x0:"0"
|
||||||
|
|
||||||
printf "%d", 0x00
|
printf "%d", 0x00
|
||||||
printf_int:"%d":0x00:"0"
|
printf_integer:"%d":PRINTF_CAST_INT:0x00:"0"
|
||||||
|
|
||||||
printf "%d", 0x000000000000000000000000000000000000000000
|
printf "%d", 0x000000000000000000000000000000000000000000
|
||||||
printf_int:"%d":0x000000000000000000000000000000000000000000:"0"
|
printf_integer:"%d":PRINTF_CAST_INT:0x000000000000000000000000000000000000000000:"0"
|
||||||
|
|
||||||
printf "%d", -0x0
|
printf "%d", -0x0
|
||||||
printf_int:"%d":-0x0:"0"
|
printf_integer:"%d":PRINTF_CAST_INT:-0x0:"0"
|
||||||
|
|
||||||
printf "%d", 1
|
printf "%d", 1
|
||||||
printf_int:"%d":1:"1"
|
printf_integer:"%d":PRINTF_CAST_INT:1:"1"
|
||||||
|
|
||||||
printf "%d", 0x1
|
printf "%d", 0x1
|
||||||
printf_int:"%d":0x1:"1"
|
printf_integer:"%d":PRINTF_CAST_INT:0x1:"1"
|
||||||
|
|
||||||
printf "%d", 0x0000000000000000000000000000000000000000001
|
printf "%d", 0x0000000000000000000000000000000000000000001
|
||||||
printf_int:"%d":0x0000000000000000000000000000000000000000001:"1"
|
printf_integer:"%d":PRINTF_CAST_INT:0x0000000000000000000000000000000000000000001:"1"
|
||||||
|
|
||||||
printf "%d", -1
|
printf "%d", -1
|
||||||
printf_int:"%d":-1:"-1"
|
printf_integer:"%d":PRINTF_CAST_INT:-1:"-1"
|
||||||
|
|
||||||
printf "%d", -0x1
|
printf "%d", -0x1
|
||||||
printf_int:"%d":-0x1:"-1"
|
printf_integer:"%d":PRINTF_CAST_INT:-0x1:"-1"
|
||||||
|
|
||||||
printf "%d", -0x0000000000000000000000000000000000000000001
|
printf "%d", -0x0000000000000000000000000000000000000000001
|
||||||
printf_int:"%d":-0x0000000000000000000000000000000000000000001:"-1"
|
printf_integer:"%d":PRINTF_CAST_INT:-0x0000000000000000000000000000000000000000001:"-1"
|
||||||
|
|
||||||
printf "%d", 2147483647
|
printf "%d", 2147483647
|
||||||
printf_int:"%d":2147483647:"2147483647"
|
printf_integer:"%d":PRINTF_CAST_INT:2147483647:"2147483647"
|
||||||
|
|
||||||
printf "%d", 0x7fffffff
|
printf "%d", 0x7fffffff
|
||||||
printf_int:"%d":0x7fffffff:"2147483647"
|
printf_integer:"%d":PRINTF_CAST_INT:0x7fffffff:"2147483647"
|
||||||
|
|
||||||
printf "%d", -2147483647
|
printf "%d", -2147483647
|
||||||
printf_int:"%d":-2147483647:"-2147483647"
|
printf_integer:"%d":PRINTF_CAST_INT:-2147483647:"-2147483647"
|
||||||
|
|
||||||
printf "%d", -0x7fffffff
|
printf "%d", -0x7fffffff
|
||||||
printf_int:"%d":-0x7fffffff:"-2147483647"
|
printf_integer:"%d":PRINTF_CAST_INT:-0x7fffffff:"-2147483647"
|
||||||
|
|
||||||
printf "%d", -2147483648
|
printf "%d", -2147483648
|
||||||
printf_int:"%d":-2147483648:"-2147483648"
|
printf_integer:"%d":PRINTF_CAST_INT:-2147483648:"-2147483648"
|
||||||
|
|
||||||
printf "%d", -0x80000000
|
printf "%d", -0x80000000
|
||||||
printf_int:"%d":-0x80000000:"-2147483648"
|
printf_integer:"%d":PRINTF_CAST_INT:-0x80000000:"-2147483648"
|
||||||
|
|
||||||
|
printf "%u", 0x80000000
|
||||||
|
printf_integer:"%u":PRINTF_CAST_INT:-0x80000000:"2147483648"
|
||||||
|
|
||||||
|
printf "%zx", 0x12345678
|
||||||
|
printf_integer:"%zx":PRINTF_CAST_SIZE_T:0x12345678:"12345678"
|
||||||
|
|
||||||
|
printf "%zu", 0x80000000
|
||||||
|
printf_integer:"%zu":PRINTF_CAST_SIZE_T:0x80000000:"2147483648"
|
||||||
|
|
||||||
|
printf "%zx", 0xffffffff
|
||||||
|
printf_integer:"%zx":PRINTF_CAST_SIZE_T:0xffffffff:"ffffffff"
|
||||||
|
|
||||||
|
printf "%zx", 0xffffffffffffffff
|
||||||
|
depends_on:SIZE_T_AT_LEAST_64_BIT
|
||||||
|
printf_integer:"%zx":PRINTF_CAST_SIZE_T:0xffffffffffffffff:"ffffffffffffffff"
|
||||||
|
|
||||||
|
printf "%lld", 0x7fffffffffffffff
|
||||||
|
printf_integer:"%lld":PRINTF_CAST_LONG_LONG:0x7fffffffffffffff:"9223372036854775807"
|
||||||
|
|
||||||
|
printf "%lld", -0x8000000000000000
|
||||||
|
printf_integer:"%lld":PRINTF_CAST_LONG_LONG:-0x8000000000000000:"-9223372036854775808"
|
||||||
|
|
||||||
|
printf "%llx", 0x102030405060708
|
||||||
|
printf_integer:"%llx":PRINTF_CAST_UNSIGNED_LONG_LONG:0x0102030405060708:"102030405060708"
|
||||||
|
|
||||||
# Test that LONG_MAX is coming out untruncated through the test framework.
|
# Test that LONG_MAX is coming out untruncated through the test framework.
|
||||||
printf "%lx", LONG_MAX
|
printf "%lx", LONG_MAX
|
||||||
|
|||||||
@@ -15,6 +15,20 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#if SIZE_MAX >= 0xffffffffffffffff
|
||||||
|
#define SIZE_T_AT_LEAST_64_BIT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
PRINTF_CAST_INT,
|
||||||
|
PRINTF_CAST_UNSIGNED,
|
||||||
|
PRINTF_CAST_SIZE_T,
|
||||||
|
PRINTF_CAST_LONG,
|
||||||
|
PRINTF_CAST_UNSIGNED_LONG,
|
||||||
|
PRINTF_CAST_LONG_LONG,
|
||||||
|
PRINTF_CAST_UNSIGNED_LONG_LONG,
|
||||||
|
} printf_cast_type_t;
|
||||||
|
|
||||||
#define NEWLINE_CHAR '\n'
|
#define NEWLINE_CHAR '\n'
|
||||||
#define SPACE_CHAR ' '
|
#define SPACE_CHAR ' '
|
||||||
#define DOUBLE_QUOTE_CHAR '"'
|
#define DOUBLE_QUOTE_CHAR '"'
|
||||||
@@ -22,18 +36,41 @@
|
|||||||
#define QUESTION_CHAR '?'
|
#define QUESTION_CHAR '?'
|
||||||
#define BACKSLASH_CHAR '\\'
|
#define BACKSLASH_CHAR '\\'
|
||||||
#define LOWERCASE_N_CHAR 'n'
|
#define LOWERCASE_N_CHAR 'n'
|
||||||
|
|
||||||
/* END_HEADER */
|
/* END_HEADER */
|
||||||
|
|
||||||
/* BEGIN_CASE */
|
/* BEGIN_CASE */
|
||||||
void printf_int(char *format, /* any format expecting one int argument, e.g. "%d" */
|
void printf_integer(char *format, /* format expecting one foo_t argument */
|
||||||
int x, char *result)
|
int type, /* enum value from printf_cast_type_t */
|
||||||
|
intmax_t value,
|
||||||
|
char *result)
|
||||||
{
|
{
|
||||||
char *output = NULL;
|
char *output = NULL;
|
||||||
const size_t n = strlen(result);
|
const size_t n = strlen(result);
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
/* Nominal case: buffer just large enough */
|
/* Nominal case: buffer just large enough */
|
||||||
TEST_CALLOC(output, n + 1);
|
TEST_CALLOC(output, n + 1);
|
||||||
TEST_EQUAL(n, mbedtls_snprintf(output, n + 1, format, x));
|
switch (type) {
|
||||||
|
case PRINTF_CAST_INT:
|
||||||
|
ret = mbedtls_snprintf(output, n + 1, format, (int) value);
|
||||||
|
break;
|
||||||
|
case PRINTF_CAST_UNSIGNED:
|
||||||
|
ret = mbedtls_snprintf(output, n + 1, format, (unsigned) value);
|
||||||
|
break;
|
||||||
|
case PRINTF_CAST_SIZE_T:
|
||||||
|
ret = mbedtls_snprintf(output, n + 1, format, (size_t) value);
|
||||||
|
break;
|
||||||
|
case PRINTF_CAST_LONG_LONG:
|
||||||
|
ret = mbedtls_snprintf(output, n + 1, format, (long long) value);
|
||||||
|
break;
|
||||||
|
case PRINTF_CAST_UNSIGNED_LONG_LONG:
|
||||||
|
ret = mbedtls_snprintf(output, n + 1, format, (unsigned long long) value);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
TEST_FAIL("Unsupported type");
|
||||||
|
}
|
||||||
|
TEST_EQUAL(n, ret);
|
||||||
TEST_MEMORY_COMPARE(result, n + 1, output, n + 1);
|
TEST_MEMORY_COMPARE(result, n + 1, output, n + 1);
|
||||||
mbedtls_free(output);
|
mbedtls_free(output);
|
||||||
output = NULL;
|
output = NULL;
|
||||||
|
|||||||
Reference in New Issue
Block a user