3
3
* test_json_parser_incremental.c
4
4
* Test program for incremental JSON parser
5
5
*
6
- * Copyright (c) 2023 , PostgreSQL Global Development Group
6
+ * Copyright (c) 2024 , PostgreSQL Global Development Group
7
7
*
8
8
* IDENTIFICATION
9
9
* src/test/modules/test_json_parser/test_json_parser_incremental.c
10
10
*
11
- * This progam tests incremental parsing of json. The input is fed into
11
+ * This program tests incremental parsing of json. The input is fed into
12
12
* the parser in very small chunks. In practice you would normally use
13
13
* much larger chunks, but doing this makes it more likely that the
14
- * full range of incement handling, especially in the lexer, is exercised.
15
- * If the "-c SIZE" option is provided, that chunk size is used instead.
14
+ * full range of increment handling, especially in the lexer, is exercised.
15
+ * If the "-c SIZE" option is provided, that chunk size is used instead
16
+ * of the default of 60.
16
17
*
17
18
* The argument specifies the file containing the JSON input.
18
19
*
31
32
#include "mb/pg_wchar.h"
32
33
#include "pg_getopt.h"
33
34
35
+ #define BUFSIZE 6000
36
+ #define DEFAULT_CHUNK_SIZE 60
37
+
34
38
typedef struct DoState
35
39
{
36
40
JsonLexContext * lex ;
@@ -67,14 +71,13 @@ JsonSemAction sem = {
67
71
int
68
72
main (int argc , char * * argv )
69
73
{
70
- /* max delicious line length is less than this */
71
- char buff [6001 ];
74
+ char buff [BUFSIZE ];
72
75
FILE * json_file ;
73
76
JsonParseErrorType result ;
74
77
JsonLexContext lex ;
75
78
StringInfoData json ;
76
79
int n_read ;
77
- size_t chunk_size = 60 ;
80
+ size_t chunk_size = DEFAULT_CHUNK_SIZE ;
78
81
struct stat statbuf ;
79
82
off_t bytes_left ;
80
83
JsonSemAction * testsem = & nullSemAction ;
@@ -88,6 +91,11 @@ main(int argc, char **argv)
88
91
{
89
92
case 'c' : /* chunksize */
90
93
sscanf (optarg , "%zu" , & chunk_size );
94
+ if (chunk_size > BUFSIZE )
95
+ {
96
+ fprintf (stderr , "chunk size cannot exceed %d\n" , BUFSIZE );
97
+ exit (1 );
98
+ }
91
99
break ;
92
100
case 's' : /* do semantic processing */
93
101
testsem = & sem ;
@@ -121,6 +129,12 @@ main(int argc, char **argv)
121
129
{
122
130
n_read = fread (buff , 1 , chunk_size , json_file );
123
131
appendBinaryStringInfo (& json , buff , n_read );
132
+
133
+ /*
134
+ * Append some trailing junk to the buffer passed to the parser. This
135
+ * helps us ensure that the parser does the right thing even if the
136
+ * chunk isn't terminated with a '\0'.
137
+ */
124
138
appendStringInfoString (& json , "1+23 trailing junk" );
125
139
bytes_left -= n_read ;
126
140
if (bytes_left > 0 )
0 commit comments