Fixed lwip_itoa (bug #51729) and added unit tests for it

This commit is contained in:
goldsimon 2017-12-23 20:35:18 +01:00
parent b07a481f66
commit c6887522fe
4 changed files with 96 additions and 9 deletions

View File

@ -201,7 +201,8 @@ lwip_itoa(char *result, size_t bufsize, int number)
char *tmp = result;
size_t res_left = bufsize;
size_t result_len;
int n = (number > 0) ? number : -number;
int pos = number >= 0;
int n = pos ? number : -number;
/* handle invalid bufsize */
if (bufsize < 2) {
@ -217,21 +218,16 @@ lwip_itoa(char *result, size_t bufsize, int number)
/* create the string in a temporary buffer since we don't know how long
it will get */
tmp = &result[bufsize - 2];
if (n == 0) {
*tmp = '0';
tmp--;
result_len++;
}
while ((n != 0) && (result_len < (bufsize - 1))) {
do {
char val = (char)('0' + (n % 10));
*tmp = val;
tmp--;
n = n / 10;
result_len++;
}
} while ((n != 0) && (result_len <= bufsize));
/* output sign first */
if (number < 0) {
if (!pos) {
*res = '-';
res++;
res_left--;

81
test/unit/core/test_def.c Normal file
View File

@ -0,0 +1,81 @@
#include "test_def.h"
#include "lwip/def.h"
#define MAGIC_UNTOUCHED_BYTE 0xfa
#define TEST_BUFSIZE 32
#define GUARD_SIZE 4
/* Setups/teardown functions */
static void
def_setup(void)
{
}
static void
def_teardown(void)
{
}
static void
def_check_range_untouched(const char *buf, size_t len)
{
size_t i;
for (i = 0; i < len; i++) {
fail_unless(buf[i] == (char)MAGIC_UNTOUCHED_BYTE);
}
}
static void test_def_itoa(int number, const char *expected)
{
char buf[TEST_BUFSIZE];
char *test_buf = &buf[GUARD_SIZE];
size_t exp_len = strlen(expected);
fail_unless(exp_len + 1 < (TEST_BUFSIZE - (2 * GUARD_SIZE)));
memset(buf, MAGIC_UNTOUCHED_BYTE, sizeof(buf));
lwip_itoa(test_buf, exp_len + 1, number);
def_check_range_untouched(buf, GUARD_SIZE);
fail_unless(test_buf[exp_len] == 0);
fail_unless(!memcmp(test_buf, expected, exp_len));
def_check_range_untouched(&test_buf[exp_len + 1], TEST_BUFSIZE - GUARD_SIZE - exp_len - 1);
/* check with too small buffer */
memset(buf, MAGIC_UNTOUCHED_BYTE, sizeof(buf));
lwip_itoa(test_buf, exp_len, number);
/* check with too large buffer */
memset(buf, MAGIC_UNTOUCHED_BYTE, sizeof(buf));
lwip_itoa(test_buf, exp_len + 4, number);
def_check_range_untouched(buf, GUARD_SIZE);
fail_unless(test_buf[exp_len] == 0);
fail_unless(!memcmp(test_buf, expected, exp_len));
def_check_range_untouched(&test_buf[exp_len + 4], TEST_BUFSIZE - GUARD_SIZE - exp_len - 4);
}
START_TEST(test_def_lwip_itoa)
{
LWIP_UNUSED_ARG(_i);
test_def_itoa(0, "0");
test_def_itoa(1, "1");
test_def_itoa(-1, "-1");
test_def_itoa(15, "15");
test_def_itoa(-15, "-15");
test_def_itoa(156, "156");
test_def_itoa(-156, "-156");
}
END_TEST
/** Create the suite including all tests for this module */
Suite *
def_suite(void)
{
testfunc tests[] = {
TESTFUNC(test_def_lwip_itoa)
};
return create_suite("DEF", tests, sizeof(tests)/sizeof(testfunc), def_setup, def_teardown);
}

View File

@ -0,0 +1,8 @@
#ifndef LWIP_HDR_TEST_DEF_H
#define LWIP_HDR_TEST_DEF_H
#include "../lwip_check.h"
Suite *def_suite(void);
#endif

View File

@ -4,6 +4,7 @@
#include "udp/test_udp.h"
#include "tcp/test_tcp.h"
#include "tcp/test_tcp_oos.h"
#include "core/test_def.h"
#include "core/test_mem.h"
#include "core/test_pbuf.h"
#include "etharp/test_etharp.h"
@ -62,6 +63,7 @@ int main(void)
udp_suite,
tcp_suite,
tcp_oos_suite,
def_suite,
mem_suite,
pbuf_suite,
etharp_suite,