Skip to content

Commit

Permalink
Add a check to avoid segfault
Browse files Browse the repository at this point in the history
Triggered by out of bound array access when retreiving the mysql encoding.
  • Loading branch information
intrip committed Aug 1, 2023
1 parent e23d40e commit 4dd1348
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 1 deletion.
1 change: 1 addition & 0 deletions ext/mysql2/mysql_enc_to_ruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,4 @@ const char *mysql2_mysql_enc_to_rb[] = {
"UTF-8"
};

#define CHARSETNR_SIZE (sizeof(mysql2_mysql_enc_to_rb)/sizeof(mysql2_mysql_enc_to_rb[0]))
2 changes: 1 addition & 1 deletion ext/mysql2/result.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static VALUE mysql2_set_field_string_encoding(VALUE val, MYSQL_FIELD field, rb_e
const char *enc_name;
int enc_index;

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

0 comments on commit 4dd1348

Please sign in to comment.