Skip to content

Commit d58062c

Browse files
committed
Fix nasty error with wrong size of the allocated structure.
1 parent 8191bf0 commit d58062c

File tree

3 files changed

+36
-37
lines changed

3 files changed

+36
-37
lines changed

Data/JsonStream/CLexer.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ defHeader :: Header
5050
defHeader = Header 0 0 0 0 0 0 0
5151

5252
instance Storable Header where
53-
sizeOf _ = 8 * sizeOf (undefined :: CInt)
53+
sizeOf _ = 7 * sizeOf (undefined :: CInt)
5454
alignment _ = sizeOf (undefined :: CInt)
5555
peek ptr = do
5656
state <- peekByteOff ptr 0

c_lib/lexer.c

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ static inline int isJnumber(char chr)
3030
}
3131

3232
// Add simple result to the result list
33-
static inline void add_simple_res(int restype, struct lexer *lexer, int length)
33+
static inline void add_simple_res(int restype, struct lexer *lexer, int length, struct lexer_result *result)
3434
{
35-
struct lexer_result *res = &lexer->result[lexer->result_num];
35+
struct lexer_result *res = &result[lexer->result_num];
3636

3737
res->restype = restype;
3838
res->startpos = lexer->position;
@@ -52,17 +52,17 @@ static inline int handle_space(const char *input, struct lexer *lexer)
5252
return LEX_OK;
5353
}
5454

55-
static inline int handle_base(const char *input, struct lexer *lexer)
55+
static inline int handle_base(const char *input, struct lexer *lexer, struct lexer_result *result)
5656
{
5757
if (handle_space(input, lexer))
5858
return LEX_OK;
5959

6060
char chr = input[lexer->position];
6161
switch (chr) {
62-
case '{': add_simple_res(RES_OPEN_BRACE, lexer, 1); lexer->position++;break;
63-
case '}': add_simple_res(RES_CLOSE_BRACE, lexer, 1); lexer->position++;break;
64-
case '[': add_simple_res(RES_OPEN_BRACKET, lexer, 1); lexer->position++;break;
65-
case ']': add_simple_res(RES_CLOSE_BRACKET, lexer, 1); lexer->position++;break;
62+
case '{': add_simple_res(RES_OPEN_BRACE, lexer, 1, result); lexer->position++;break;
63+
case '}': add_simple_res(RES_CLOSE_BRACE, lexer, 1, result); lexer->position++;break;
64+
case '[': add_simple_res(RES_OPEN_BRACKET, lexer, 1, result); lexer->position++;break;
65+
case ']': add_simple_res(RES_CLOSE_BRACKET, lexer, 1, result); lexer->position++;break;
6666
case '"': lexer->current_state = STATE_STRING; lexer->state_data = 0; lexer->position++;return LEX_OK;
6767
case 't': lexer->current_state = STATE_TRUE; lexer->state_data = 1; lexer->position++;return LEX_OK;
6868
case 'f': lexer->current_state = STATE_FALSE; lexer->state_data = 1; lexer->position++;return LEX_OK;
@@ -80,14 +80,15 @@ static inline int handle_base(const char *input, struct lexer *lexer)
8080
return LEX_OK;
8181
}
8282

83-
static inline int handle_ident(const char *input, struct lexer *lexer, const char *ident, int idtype)
83+
static inline int handle_ident(const char *input, struct lexer *lexer, const char *ident, int idtype,
84+
struct lexer_result *result)
8485
{
8586
while (lexer->position < lexer->length) {
8687
char chr = input[lexer->position];
8788
if (!ident[lexer->state_data]) {
8889
// Check that the next character is allowed
8990
if (isempty(chr) || chr == ']' || chr == '}') {
90-
add_simple_res(idtype, lexer, lexer->state_data);
91+
add_simple_res(idtype, lexer, lexer->state_data, result);
9192
lexer->current_state = STATE_BASE;
9293
return LEX_OK;
9394
} else {
@@ -104,7 +105,7 @@ static inline int handle_ident(const char *input, struct lexer *lexer, const cha
104105
}
105106

106107
/* Read a number; compute the number if the 'int' type can hold it */
107-
int handle_number(const char *input, struct lexer *lexer)
108+
int handle_number(const char *input, struct lexer *lexer, struct lexer_result *result)
108109
{
109110
/* Just eat characters that can be numbers and feed them to a table */
110111
// Copy the character to buffer
@@ -143,7 +144,7 @@ int handle_number(const char *input, struct lexer *lexer)
143144
}
144145
}
145146

146-
struct lexer_result *res = &lexer->result[lexer->result_num];
147+
struct lexer_result *res = &result[lexer->result_num];
147148
res->adddata = lexer->state_data;
148149
if (lexer->position == lexer->length) {
149150
res->restype = RES_NUMBER_PARTIAL;
@@ -176,14 +177,14 @@ static inline int safechar(char x) {
176177
}
177178

178179
/* Handle beginning of a string, the '"' is already stripped */
179-
int handle_string(const char *input, struct lexer *lexer)
180+
int handle_string(const char *input, struct lexer *lexer, struct lexer_result *result)
180181
{
181182
int startposition = lexer->position;
182183
char ch;
183184
for (ch=input[lexer->position]; lexer->position < lexer->length && safechar(ch); ch = input[++lexer->position])
184185
;
185186

186-
struct lexer_result *res = &lexer->result[lexer->result_num];
187+
struct lexer_result *res = &result[lexer->result_num];
187188
res->startpos = startposition;
188189
res->length = lexer->position - startposition;
189190
if (lexer->position == lexer->length || input[lexer->position] == '\\') {
@@ -214,7 +215,7 @@ int handle_string(const char *input, struct lexer *lexer)
214215
}
215216

216217
/* Handle \uxxxx syntax */
217-
static int handle_string_uni(const char *input, struct lexer *lexer)
218+
static int handle_string_uni(const char *input, struct lexer *lexer, struct lexer_result *result)
218219
{
219220
char chr = input[lexer->position];
220221
lexer->state_data_2 *= 16;
@@ -230,7 +231,7 @@ static int handle_string_uni(const char *input, struct lexer *lexer)
230231
lexer->position += 1;
231232
if (lexer->state_data == 4) {
232233
// Emit the result
233-
struct lexer_result *res = &lexer->result[lexer->result_num];
234+
struct lexer_result *res = &result[lexer->result_num];
234235
res->startpos = lexer->position;
235236
res->length = 0;
236237
res->restype = RES_STRING_UNI;
@@ -244,9 +245,9 @@ static int handle_string_uni(const char *input, struct lexer *lexer)
244245
}
245246

246247
// Add a character to result, move position forward, change state back to string
247-
static inline void emitchar(char ch, struct lexer *lexer)
248+
static inline void emitchar(char ch, struct lexer *lexer, struct lexer_result *result)
248249
{
249-
struct lexer_result *res = &lexer->result[lexer->result_num];
250+
struct lexer_result *res = &result[lexer->result_num];
250251

251252
res->restype = RES_STRING_PARTIAL;
252253
res->startpos = lexer->position;
@@ -259,18 +260,18 @@ static inline void emitchar(char ch, struct lexer *lexer)
259260
lexer->state_data = 1; // Set the string is in partial data
260261
}
261262

262-
int handle_specchar(const char *input, struct lexer *lexer)
263+
int handle_specchar(const char *input, struct lexer *lexer, struct lexer_result *result)
263264
{
264265
char chr = input[lexer->position];
265266
switch (chr) {
266-
case '"': emitchar('"', lexer);break;
267-
case '\\':emitchar('\\', lexer);break;
268-
case '/':emitchar('/', lexer);break;
269-
case 'b':emitchar('\b', lexer);break;
270-
case 'f':emitchar('\f', lexer);break;
271-
case 'n':emitchar('\n', lexer);break;
272-
case 'r':emitchar('\r', lexer);break;
273-
case 't':emitchar('\t', lexer);break;
267+
case '"': emitchar('"', lexer, result);break;
268+
case '\\':emitchar('\\', lexer, result);break;
269+
case '/':emitchar('/', lexer, result);break;
270+
case 'b':emitchar('\b', lexer, result);break;
271+
case 'f':emitchar('\f', lexer, result);break;
272+
case 'n':emitchar('\n', lexer, result);break;
273+
case 'r':emitchar('\r', lexer, result);break;
274+
case 't':emitchar('\t', lexer, result);break;
274275
case 'u':
275276
lexer->current_state = STATE_STRING_UNI;
276277
lexer->state_data = 0;
@@ -285,7 +286,6 @@ int handle_specchar(const char *input, struct lexer *lexer)
285286

286287
int lex_json(const char *input, struct lexer *lexer, struct lexer_result *result)
287288
{
288-
lexer->result = result;
289289
int res = LEX_OK;
290290
static void* dispatch_table[] = {
291291
&&state_base, &&state_string, &&state_number, &&state_true,
@@ -300,28 +300,28 @@ int lex_json(const char *input, struct lexer *lexer, struct lexer_result *result
300300

301301
DISPATCH();
302302
state_base:
303-
res = handle_base(input, lexer);
303+
res = handle_base(input, lexer, result);
304304
DISPATCH();
305305
state_string:
306-
res = handle_string(input, lexer);
306+
res = handle_string(input, lexer, result);
307307
DISPATCH();
308308
state_number:
309-
res = handle_number(input, lexer);
309+
res = handle_number(input, lexer, result);
310310
DISPATCH();
311311
state_true:
312-
res = handle_ident(input, lexer, "true", RES_TRUE);
312+
res = handle_ident(input, lexer, "true", RES_TRUE, result);
313313
DISPATCH();
314314
state_false:
315-
res = handle_ident(input, lexer, "false", RES_FALSE);
315+
res = handle_ident(input, lexer, "false", RES_FALSE, result);
316316
DISPATCH();
317317
state_null:
318-
res = handle_ident(input, lexer, "null", RES_NULL);
318+
res = handle_ident(input, lexer, "null", RES_NULL, result);
319319
DISPATCH();
320320
state_string_specchar:
321-
res = handle_specchar(input, lexer);
321+
res = handle_specchar(input, lexer, result);
322322
DISPATCH();
323323
state_string_uni:
324-
res = handle_string_uni(input, lexer);
324+
res = handle_string_uni(input, lexer, result);
325325
DISPATCH();
326326

327327
return res;

c_lib/lexer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ struct lexer {
4646

4747
int result_num;
4848
int result_limit;
49-
struct lexer_result *result;
5049
};
5150

5251
#define LEX_OK 0

0 commit comments

Comments
 (0)