Skip to content

Commit b7f1e2d

Browse files
committed
Fix encoding handling in C lexer
1 parent 3a548f8 commit b7f1e2d

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
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: 21 additions & 1 deletion
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?
@@ -56,7 +76,7 @@ def initialize(query_string, filename, trace)
5676
def result
5777
if @result.nil?
5878
@tokens = @trace.lex(query_string: @query_string) do
59-
GraphQL::CParser::Lexer.tokenize(@query_string)
79+
GraphQL::CParser.scan_with_c(@query_string)
6080
end
6181
@trace.parse(query_string: @query_string) do
6282
c_parse

0 commit comments

Comments
 (0)