Skip to content

Commit 8a8b5e3

Browse files
authored
Merge pull request #4480 from rmosolgo/cparser-encoding-fix
Fix encoding handling in C lexer
2 parents da41589 + 6eba091 commit 8a8b5e3

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

graphql-c_parser/ext/graphql_c_parser_ext/graphql_c_parser_ext.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "graphql_c_parser_ext.h"
22

3-
VALUE GraphQL_CParser_Lexer_tokenize(VALUE self, VALUE query_string) {
3+
VALUE GraphQL_CParser_Lexer_tokenize_with_c(VALUE self, VALUE query_string) {
44
return tokenize(query_string);
55
}
66

@@ -13,7 +13,7 @@ void Init_graphql_c_parser_ext() {
1313
VALUE GraphQL = rb_define_module("GraphQL");
1414
VALUE CParser = rb_define_module_under(GraphQL, "CParser");
1515
VALUE Lexer = rb_define_module_under(CParser, "Lexer");
16-
rb_define_singleton_method(Lexer, "tokenize", GraphQL_CParser_Lexer_tokenize, 1);
16+
rb_define_singleton_method(Lexer, "tokenize_with_c", GraphQL_CParser_Lexer_tokenize_with_c, 1);
1717
setup_static_token_variables();
1818

1919
VALUE Parser = rb_define_class_under(CParser, "Parser", rb_cObject);

graphql-c_parser/lib/graphql/c_parser.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,26 @@ def self.prepare_parse_error(message, parser)
4040
GraphQL::ParseError.new(message, line, col, parser.query_string, filename: parser.filename)
4141
end
4242

43+
module Lexer
44+
def self.tokenize(graphql_string)
45+
if !(graphql_string.encoding == Encoding::UTF_8 || graphql_string.ascii_only?)
46+
graphql_string = graphql_string.dup.force_encoding(Encoding::UTF_8)
47+
end
48+
if !graphql_string.valid_encoding?
49+
return [
50+
[
51+
:BAD_UNICODE_ESCAPE,
52+
1,
53+
1,
54+
graphql_string,
55+
nil
56+
]
57+
]
58+
end
59+
tokenize_with_c(graphql_string)
60+
end
61+
end
62+
4363
class Parser
4464
def initialize(query_string, filename, trace)
4565
if query_string.nil?

0 commit comments

Comments
 (0)