Skip to content

Commit

Permalink
Copy input in json_parse_buffer
Browse files Browse the repository at this point in the history
The ovis_json.h file included a #define that would collide
with flex/bison parsers attempting to use this library.

The input buffer is copied because the scanner modifies the
contents, and requires two terminating '\0'.
  • Loading branch information
tom95858 committed Jul 20, 2020
1 parent af58d0a commit a16bb54
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
1 change: 0 additions & 1 deletion lib/src/ovis_json/ovis_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,4 @@ extern jbuf_t jbuf_append_str(jbuf_t jb, const char *fmt, ...);
extern jbuf_t jbuf_append_va(jbuf_t jb, const char *fmt, va_list ap);
extern void jbuf_free(jbuf_t jb);

#define YYSTYPE json_entity_t
#endif
22 changes: 22 additions & 0 deletions lib/src/ovis_json/ovis_json_lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <ovis_json.h>
#include "ovis_json_parser.h"

#define YYSTYPE json_entity_t

struct yyguts_t;
void update_yylloc(YYLTYPE *, struct yyguts_t *);
void init_yylloc(YYLTYPE *, struct yyguts_t *);
Expand Down Expand Up @@ -125,3 +127,23 @@ void init_yylloc(YYLTYPE *yyl, struct yyguts_t *yyg)
yyl->first_column = yyl->last_column = 0;
yylineno = 0;
}

int json_parse_buffer(json_parser_t p, char *buf, size_t buf_len, json_entity_t *pentity)
{
int rc;
*pentity = NULL;
char *nbuf = malloc(buf_len + 2);
if (!nbuf)
return ENOMEM;
memcpy(nbuf, buf, buf_len);
nbuf[buf_len] = YY_END_OF_BUFFER_CHAR;
nbuf[buf_len+1] = YY_END_OF_BUFFER_CHAR;
if (NULL == yy_scan_buffer(buf, buf_len + 2, p->scanner)) {
rc = EINVAL;
goto out;
}
rc = yyparse(p, nbuf, buf_len + 2, pentity);
out:
free(nbuf);
return rc;
}
18 changes: 3 additions & 15 deletions lib/src/ovis_json/ovis_json_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include <string.h>
#include <errno.h>
#include "ovis_json.h"

#define YYLTYPE struct json_loc_s
#define YYSTYPE json_entity_t

void yyerror(YYLTYPE *yylloc, json_parser_t parser, char *input, size_t input_len,
json_entity_t *pentity, const char *str)
Expand Down Expand Up @@ -127,7 +129,7 @@ item_list: /* empty */ { $$ = new_list_val(); }
;

%%
#include <assert.h>

json_parser_t json_parser_new(size_t user_data) {
json_parser_t p = calloc(1, sizeof *p + user_data);
if (p)
Expand All @@ -141,17 +143,3 @@ void json_parser_free(json_parser_t parser)
free(parser);
}

int json_parse_buffer(json_parser_t p, char *buf, size_t buf_len, json_entity_t *pentity)
{
int rc;
*pentity = NULL;
/* NB: Force two 0x00 on the end of the buffer for the scanner. If
* the user didn't allocate enough memory, this could corrupt
* memory */
buf[buf_len] = 0x00;
buf[buf_len+1] = 0x00;
assert(NULL != yy_scan_buffer(buf, buf_len+2, p->scanner));
rc = yyparse(p, buf, buf_len, pentity);
return rc;
}

0 comments on commit a16bb54

Please sign in to comment.