Skip to content

Commit 4dd1348

Browse files
committed
Add a check to avoid segfault
Triggered by out of bound array access when retreiving the mysql encoding.
1 parent e23d40e commit 4dd1348

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

ext/mysql2/mysql_enc_to_ruby.h

+1
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,4 @@ const char *mysql2_mysql_enc_to_rb[] = {
244244
"UTF-8"
245245
};
246246

247+
#define CHARSETNR_SIZE (sizeof(mysql2_mysql_enc_to_rb)/sizeof(mysql2_mysql_enc_to_rb[0]))

ext/mysql2/result.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static VALUE mysql2_set_field_string_encoding(VALUE val, MYSQL_FIELD field, rb_e
158158
const char *enc_name;
159159
int enc_index;
160160

161-
enc_name = mysql2_mysql_enc_to_rb[field.charsetnr-1];
161+
enc_name = (field.charsetnr-1 < CHARSETNR_SIZE) ? mysql2_mysql_enc_to_rb[field.charsetnr-1] : NULL;
162162
if (enc_name != NULL) {
163163
/* use the field encoding we were able to match */
164164
enc_index = rb_enc_find_index(enc_name);

0 commit comments

Comments
 (0)