Skip to content

Commit 86c352d

Browse files
authored
Merge pull request #919 from sodabrew/typedef_my_bool
Revisit PR #840
2 parents a2440a7 + a799b0b commit 86c352d

File tree

6 files changed

+21
-9
lines changed

6 files changed

+21
-9
lines changed

ext/mysql2/client.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static VALUE rb_set_ssl_mode_option(VALUE self, VALUE setting) {
114114
int val = NUM2INT( setting );
115115
if (version >= 50703 && version < 50711) {
116116
if (val == SSL_MODE_DISABLED || val == SSL_MODE_REQUIRED) {
117-
bool b = ( val == SSL_MODE_REQUIRED );
117+
my_bool b = ( val == SSL_MODE_REQUIRED );
118118
int result = mysql_options( wrapper->client, MYSQL_OPT_SSL_ENFORCE, &b );
119119
return INT2NUM(result);
120120
} else {
@@ -526,7 +526,7 @@ static VALUE do_send_query(void *args) {
526526
*/
527527
static void *nogvl_read_query_result(void *ptr) {
528528
MYSQL * client = ptr;
529-
bool res = mysql_read_query_result(client);
529+
my_bool res = mysql_read_query_result(client);
530530

531531
return (void *)(res == 0 ? Qtrue : Qfalse);
532532
}
@@ -846,7 +846,7 @@ static VALUE _mysql_client_options(VALUE self, int opt, VALUE value) {
846846
const void *retval = NULL;
847847
unsigned int intval = 0;
848848
const char * charval = NULL;
849-
bool boolval;
849+
my_bool boolval;
850850

851851
GET_CLIENT(self);
852852

ext/mysql2/extconf.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,17 @@ def add_ssl_defines(header)
105105
add_ssl_defines(mysql_h)
106106
have_struct_member('MYSQL', 'net.vio', mysql_h)
107107
have_struct_member('MYSQL', 'net.pvio', mysql_h)
108+
108109
# These constants are actually enums, so they cannot be detected by #ifdef in C code.
109110
have_const('MYSQL_ENABLE_CLEARTEXT_PLUGIN', mysql_h)
110111
have_const('SERVER_QUERY_NO_GOOD_INDEX_USED', mysql_h)
111112
have_const('SERVER_QUERY_NO_INDEX_USED', mysql_h)
112113
have_const('SERVER_QUERY_WAS_SLOW', mysql_h)
113114

115+
# my_bool is replaced by C99 bool in MySQL 8.0, but we want
116+
# to retain compatibility with the typedef in earlier MySQLs.
117+
have_type('my_bool', mysql_h)
118+
114119
# This is our wishlist. We use whichever flags work on the host.
115120
# -Wall and -Wextra are included by default.
116121
wishlist = [

ext/mysql2/mysql2_ext.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ void Init_mysql2(void);
2828
#define RB_MYSQL_UNUSED
2929
#endif
3030

31+
/* MySQL 8.0 replaces my_bool with C99 bool. Earlier versions of MySQL had
32+
* a typedef to char. Gem users reported failures on big endian systems when
33+
* using C99 bool types with older MySQLs due to mismatched behavior. */
34+
#ifndef HAVE_TYPE_MY_BOOL
35+
#include <stdbool.h>
36+
typedef bool my_bool;
37+
#endif
38+
3139
#include <client.h>
3240
#include <statement.h>
3341
#include <result.h>

ext/mysql2/result.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ static void rb_mysql_result_alloc_result_buffers(VALUE self, MYSQL_FIELD *fields
218218
if (wrapper->result_buffers != NULL) return;
219219

220220
wrapper->result_buffers = xcalloc(wrapper->numberOfFields, sizeof(MYSQL_BIND));
221-
wrapper->is_null = xcalloc(wrapper->numberOfFields, sizeof(bool));
222-
wrapper->error = xcalloc(wrapper->numberOfFields, sizeof(bool));
221+
wrapper->is_null = xcalloc(wrapper->numberOfFields, sizeof(my_bool));
222+
wrapper->error = xcalloc(wrapper->numberOfFields, sizeof(my_bool));
223223
wrapper->length = xcalloc(wrapper->numberOfFields, sizeof(unsigned long));
224224

225225
for (i = 0; i < wrapper->numberOfFields; i++) {

ext/mysql2/result.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#ifndef MYSQL2_RESULT_H
22
#define MYSQL2_RESULT_H
3-
#include <stdbool.h>
43

54
void init_mysql2_result(void);
65
VALUE rb_mysql_result_to_obj(VALUE client, VALUE encoding, VALUE options, MYSQL_RES *r, VALUE statement);
@@ -22,8 +21,8 @@ typedef struct {
2221
mysql_client_wrapper *client_wrapper;
2322
/* statement result bind buffers */
2423
MYSQL_BIND *result_buffers;
25-
bool *is_null;
26-
bool *error;
24+
my_bool *is_null;
25+
my_bool *error;
2726
unsigned long *length;
2827
} mysql2_result_wrapper;
2928

ext/mysql2/statement.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ VALUE rb_mysql_stmt_new(VALUE rb_client, VALUE sql) {
115115

116116
// set STMT_ATTR_UPDATE_MAX_LENGTH attr
117117
{
118-
bool truth = 1;
118+
my_bool truth = 1;
119119
if (mysql_stmt_attr_set(stmt_wrapper->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &truth)) {
120120
rb_raise(cMysql2Error, "Unable to initialize prepared statement: set STMT_ATTR_UPDATE_MAX_LENGTH");
121121
}

0 commit comments

Comments
 (0)