1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
/*
Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "port/sys.h"
#define WOLF_TEST_SNPRINTF
#include "port/string.h" /* for strcmp */
#include "port/stdio.c" /* for snprintf */
#include <stdlib.h> /* for exit, EXIT_SUCCESS, free */
int main( void ) {
char buf1[255];
int res1;
char buf2[255];
int res2;
char buf3[255];
int res3;
char small_buf1[10];
int small_res1;
char small_buf2[10];
int small_res2;
/* test if we get the same things for the most important format strings */
res1 = rpl_snprintf( buf1, 255, "test with %s %d %lu", "test", 12, 234443334L );
res2 = snprintf( buf2, 255, "test with %s %d %lu", "test", 12, 234443334L );
res3 = sprintf( buf3, "test with %s %d %lu", "test", 12, 234443334L );
printf( "RPL snprintf: %d %s\nsnprintf: %d %s\nsprintf: %d %s\n",
res1, buf1, res2, buf2, res3, buf3 );
if( res1 != res2 || res1 != res3 || res1 != 27 ) return EXIT_FAILURE;
if( strcmp( buf1, buf2 ) != 0 ||
strcmp( buf1, buf2 ) != 0 ) return EXIT_FAILURE;
/* most important, does the overflow protection work */
small_res1 = rpl_snprintf( small_buf1, 10, "%s", "12345678901234" );
small_res2 = snprintf( small_buf2, 10, "%s", "12345678901234" );
printf( "RPL snprintf: %d %s\nsnprintf: %d %s\n",
small_res1, small_buf1, small_res2, small_buf2 );
if( small_res1 != small_res2 || small_res1 != 14 ) return EXIT_FAILURE;
if( small_buf1[9] != '\0' || small_buf2[9] != '\0' ) return EXIT_FAILURE;
if( strcmp( small_buf1, small_buf2 ) != 0 ) return EXIT_FAILURE;
if( strcmp( small_buf1, "123456789" ) != 0 ) return EXIT_FAILURE;
return EXIT_SUCCESS;
}
|