source: tests/io/manipulatorsInput.cfa @ 2fa0237

Last change on this file since 2fa0237 was 2fa0237, checked in by Michael Brooks <mlbrooks@…>, 9 months ago

Fix cstring input length interpretation issue that had a buffer overflow case.

The cases added to the manipulatorsInput test are runnable against an old libcfa build. In this setup, the test fails with an illustration of the bug.

The testing in this commit drives the following inputs through a length-8 buffer.

  • 123456
  • 123456789

The obviously-missing cases, like 1234567, will be added later.
They will accompany fixes for further bugs not solved yet.

  • Property mode set to 100644
File size: 6.2 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2019 University of Waterloo
3//
4// manipulatorsInput.cfa --
5//
6// Author           : Peter A. Buhr
7// Created On       : Sat Jun  8 17:58:54 2019
8// Last Modified By : Peter A. Buhr
9// Last Modified On : Sat Sep  2 14:27:46 2023
10// Update Count     : 65
11//
12
13#include <fstream.hfa>
14#include <stdio.h>                                      // scanf
15
16int main() {
17        {
18                // Upfront checks to ensure buffer safety.  Once these pass, the simpler `wdi(sizeof(s),s)`
19                // usage, as in the scanf alignment cases below, is justified.
20                struct {
21                        char buf[8];
22                        char canary;
23                } data;
24                static_assert( sizeof(data.buf) == 8 );
25                static_assert( &data.buf[8] == &data.canary );  // canary comes right after buf
26
27                void rep(const char* casename) {
28                        data.canary = 42;
29                        bool caught = false;
30                        try {
31                                sin | wdi( sizeof(data.buf), data.buf );
32                        } catch (cstring_length*) {
33                                caught = true;
34                        }
35                        printf( "%s \"%s\"", casename, data.buf );
36                        if ( caught ) {
37                                printf(", exception occurred");
38                        }
39                        if ( data.canary == 42 ) {
40                                printf(", canary ok");
41                        } else {
42                                printf(", canary overwritten to %d", data.canary);
43                        }
44                        printf("\n");
45                }
46
47                rep("pre1");
48                rep("pre2a");
49                rep("pre2b");
50                scanf("\n");  // next test does not start with %s so does not tolerate leading whitespace
51        }
52        {
53                char s[] = "yyyyyyyyyyyyyyyyyyyy";
54                const char sk[] = "abc";
55                scanf( "abc " ); scanf( sk ); for ( 5 ) scanf( "%*c" ); printf( "1 %s\n", s );
56                scanf( "%s", s );                                                               printf( "2 %s\n", s );
57                scanf( "%*s" );                                                                 printf( "3 %s\n", s );
58                scanf( "%8s", s );                                                              printf( "4 %s\n", s );
59                scanf( "%*8s" );                                                                printf( "5 %s\n", s );
60
61                scanf( "%[abc]", s );                                                   printf( "6 %s\n", s );
62                scanf( "%[^abc]", s );                                                  printf( "7 %s\n", s );
63                scanf( "%*[abc]" );                                                             printf( "8 %s\n", s );
64                scanf( "%*[^abc]" );                                                    printf( "9 %s\n", s );
65                scanf( "%8[abc]", s );                                                  printf( "10 %s\n", s );
66                scanf( "%8[^abc]", s );                                                 printf( "11 %s\n", s );
67                scanf( "%*8[abc]" );                                                    printf( "12 %s\n", s );
68                scanf( "%*8[^abc]" );                                                   printf( "13 %s\n", s );
69        }
70        {
71                char s[] = "yyyyyyyyyyyyyyyyyyyy";
72                char sk[] = "abc";
73                sin | "abc " | skip( sk ) | skip( 5 );                  sout | "1" | s;
74                sin | wdi( sizeof(s), s );                                              sout | "2" | s;
75                sin | ignore( s );                                                              sout | "3" | s;
76                sin | wdi( sizeof(s), 8, s );                                   sout | "4" | s;
77                sin | ignore( wdi( sizeof(s), 8, s ) );                 sout | "5" | s;
78
79                sin | incl( "abc", wdi( sizeof(s), s ) );               sout | "6" | s;
80                sin | excl( "abc", wdi( sizeof(s), s ) );               sout | "7" | s;
81                sin | ignore( incl( "abc", wdi( sizeof(s), s ) ) ); sout | "8" | s;
82                sin | ignore( excl( "abc", wdi( sizeof(s), s ) ) ); sout | "9" | s;
83                sin | incl( "abc", wdi( sizeof(s), 8, s ) );    sout | "10" | s;
84                sin | excl( "abc", wdi( sizeof(s), 8, s ) );    sout | "11" | s;
85                sin | ignore( incl( "abc", wdi( sizeof(s), 8, s ) ) ); sout | "12" | s;
86                sin | ignore( excl( "abc", wdi( sizeof(s), 8, s ) ) ); sout | "13" | s;
87        }
88    /* Keep harmonized with collections/string-istream-manip */
89        {
90                char c;
91                sin | c;                                                                                sout | c;
92                sin | ignore( c );                                                              sout | c;
93
94                signed char sc;
95                sin | sc;                                                                               sout | sc;
96                sin | wdi( 3, sc );                                                             sout | sc;
97                sin | ignore( sc );                                                             sout | sc;
98                sin | ignore( wdi( 3, sc ) );                                   sout | sc;
99
100                unsigned char usc;
101                sin | usc;                                                                              sout | usc;
102                sin | wdi( 3, usc );                                                    sout | usc;
103                sin | ignore( usc );                                                    sout | usc;
104                sin | ignore( wdi( 3, usc ) );                                  sout | usc;
105
106                signed short int ssi;
107                sin | ssi;                                                                              sout | ssi;
108                sin | wdi( 3, ssi );                                                    sout | ssi;
109                sin | ignore( ssi );                                                    sout | ssi;
110                sin | ignore( wdi( 3, ssi ) );                                  sout | ssi;
111
112                unsigned short int usi;
113                sin | usi;                                                                              sout | usi;
114                sin | wdi( 3, usi );                                                    sout | usi;
115                sin | ignore( usi );                                                    sout | usi;
116                sin | ignore( wdi( 3, usi ) );                                  sout | usi;
117
118                signed int si;
119                sin | si;                                                                               sout | si;
120                sin | wdi( 3, si );                                                             sout | si;
121                sin | ignore( si );                                                             sout | si;
122                sin | ignore( wdi( 3, si ) );                                   sout | si;
123
124                unsigned int ui;
125                sin | ui;                                                                               sout | ui;
126                sin | wdi( 3, ui );                                                             sout | ui;
127                sin | ignore( ui );                                                             sout | ui;
128                sin | ignore( wdi( 3, ui ) );                                   sout | ui;
129
130                signed long int sli;
131                sin | sli;                                                                              sout | sli;
132                sin | wdi( 3, sli );                                                    sout | sli;
133                sin | ignore( sli );                                                    sout | sli;
134                sin | ignore( wdi( 3, sli ) );                                  sout | sli;
135
136                unsigned long int uli;
137                sin | uli;                                                                              sout | uli;
138                sin | wdi( 3, uli );                                                    sout | uli;
139                sin | ignore( uli );                                                    sout | uli;
140                sin | ignore( wdi( 3, uli ) );                                  sout | uli;
141
142                signed long long int slli;
143                sin | slli;                                                                             sout | slli;
144                sin | wdi( 3, slli );                                                   sout | slli;
145                sin | ignore( slli );                                                   sout | slli;
146                sin | ignore( wdi( 3, slli ) );                                 sout | slli;
147
148                unsigned long long int ulli;
149                sin | ulli;                                                                             sout | ulli;
150                sin | wdi( 3, ulli );                                                   sout | ulli;
151                sin | ignore( ulli );                                                   sout | ulli;
152                sin | ignore( wdi( 3, ulli ) );                                 sout | ulli;
153
154                float f;
155                sin | f;                                                                                sout | f;
156                sin | wdi( 8, f );                                                              sout | f;
157                sin | ignore( f );                                                              sout | f;
158                sin | ignore( wdi( 8, f ) );                                    sout | f;
159
160                double d;
161                sin | d;                                                                                sout | d;
162                sin | wdi( 8, d );                                                              sout | d;
163                sin | ignore( d );                                                              sout | d;
164                sin | ignore( wdi( 8, d ) );                                    sout | d;
165
166                long double ld;
167                sin | ld;                                                                               sout | ld;
168                sin | wdi( 8, ld );                                                             sout | ld;
169                sin | ignore( ld );                                                             sout | ld;
170                sin | ignore( wdi( 8, ld ) );                                   sout | ld;
171
172                float _Complex fc;
173                sin | fc;                                                                               sout | fc;
174                sin | wdi( 8, fc );                                                             sout | fc;
175                sin | ignore( fc );                                                             sout | fc;
176                sin | ignore( wdi( 8, fc ) );                                   sout | fc;
177
178                double _Complex dc;
179                sin | dc;                                                                               sout | dc;
180                sin | wdi( 8, dc );                                                             sout | dc;
181                sin | ignore( dc );                                                             sout | dc;
182                sin | ignore( wdi( 8, dc ) );                                   sout | dc;
183
184                long double _Complex ldc;
185                sin | ldc;                                                                              sout | ldc;
186                sin | wdi( 8, ldc );                                                    sout | ldc;
187                sin | ignore( ldc );                                                    sout | ldc;
188                sin | ignore( wdi( 8, ldc ) );                                  sout | ldc;
189        }
190        #if defined( __SIZEOF_INT128__ )
191        {
192                int128 val;
193                for ( 15 ) {
194                        sin | val;
195                        sout | val;
196                }
197        }
198        #endif // __SIZEOF_INT128__
199} // main
200
201// Local Variables: //
202// tab-width: 4 //
203// compile-command: "cfa -Wall -Wextra manipulatorsInput.cfa" //
204// End: //
Note: See TracBrowser for help on using the repository browser.