Skip to content

Commit

Permalink
avoid strlen() on raw string
Browse files Browse the repository at this point in the history
  • Loading branch information
coolbutuseless committed Jan 19, 2024
1 parent 8c1e882 commit 9368d1f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/R-yyjson-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1831,10 +1831,10 @@ void output_verbose_error(const char *str, yyjson_read_err err) {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SEXP parse_json_from_str(const char *str, parse_options *opt) {
SEXP parse_json_from_str(const char *str, size_t len, parse_options *opt) {

yyjson_read_err err;
yyjson_doc *doc = yyjson_read_opts((char *)str, strlen(str), opt->yyjson_read_flag, NULL, &err);
yyjson_doc *doc = yyjson_read_opts((char *)str, len, opt->yyjson_read_flag, NULL, &err);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// If doc is NULL, then an error occurred during parsing.
Expand Down Expand Up @@ -1920,7 +1920,7 @@ SEXP parse_from_str_(SEXP str_, SEXP parse_opts_) {
const char *str = (const char *)CHAR( STRING_ELT(str_, 0) );
parse_options opt = create_parse_options(parse_opts_);

return parse_json_from_str(str, &opt);
return parse_json_from_str(str, strlen(str), &opt);
}

//===========================================================================
Expand All @@ -1936,7 +1936,7 @@ SEXP parse_from_raw_(SEXP raw_, SEXP parse_opts_) {
// rather than running over into dead space after the raw string ends
opt.yyjson_read_flag |= YYJSON_READ_STOP_WHEN_DONE;

return parse_json_from_str(str, &opt);
return parse_json_from_str(str, (size_t)length(raw_), &opt);
}

//===========================================================================
Expand Down
2 changes: 1 addition & 1 deletion src/R-yyjson-parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ typedef struct {
#define ERR_CONTEXT 20

parse_options create_parse_options(SEXP parse_opts_);
SEXP parse_json_from_str(const char *str, parse_options *opt);
SEXP parse_json_from_str(const char *str, size_t len, parse_options *opt);


unsigned int update_type_bitset(unsigned int type_bitset, yyjson_val *val, parse_options *opt);
Expand Down

0 comments on commit 9368d1f

Please sign in to comment.