Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

many portability issues in printf format specifiers #20

Open
GoogleCodeExporter opened this issue Dec 8, 2015 · 0 comments
Open

many portability issues in printf format specifiers #20

GoogleCodeExporter opened this issue Dec 8, 2015 · 0 comments

Comments

@GoogleCodeExporter
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant