slothg / cmockery

Automatically exported from code.google.com/p/cmockery
0 stars 0 forks source link

many portability issues in printf format specifiers #20

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. create the following unit test of cmockery:

/***
* @unit_test assert_in_range with signed int.
*/
void test_assert_in_range_signed_int(void **state) {
    int value;
    int lower_bound;
    int upper_bound;
    value = INT_MIN;
    lower_bound = INT_MIN;
    upper_bound = INT_MAX;
    fprintf(stderr, "             lower bound: %d upper bound: %d \n", 
            lower_bound, upper_bound);
    assert_in_range(value, lower_bound, upper_bound);
    value = INT_MAX;
    assert_in_range(value, lower_bound, upper_bound);
    value = 0;
    assert_in_range(value, lower_bound, upper_bound);
}

2. Compile and run on i386
3. Observe output:
[ RUN      ] test_assert_in_range_signed_int
            lower bound: -2147483648 upper bound: 2147483647 
-2147483648 is not within the range 0--2147483648
ERROR: /Users/smb/src/cmockery-staging/osx/../src/unit_test/unit_test.c:235 
Failure!

Note that the bounds on the range are odd.

What is the expected output? What do you see instead?

Fixing the printf format specification string in 
integer_in_range_display_error() to reflect the 
actual size and type of the arguments (unsigned long longs) gives a better 
diagnosis of the 
problem. Changing the print_error() call to:

   print_error("%llu is not within the range %llu-%llu\n", value, range_min,
               range_max);

gives us this on i386:

[ RUN      ] test_assert_in_range_signed_int
            lower bound: -2147483648 upper bound: 2147483647 
2147483648 is not within the range 2147483648-2147483647
ERROR: /Users/smb/src/cmockery-staging/osx/../src/unit_test/unit_test.c:235 
Failure!

What version of the product are you using? On what operating system?

Please provide any additional information below.

There are many other cases beside the integer_in_range_display_error() function 
where the printf 
format specification assumes a non-portable size. Other examples include 
printing pointers and 
size_t.

Original issue reported on code.google.com by Stephen....@gmail.com on 2 Mar 2010 at 1:23