Skip to content

Commit bb72702

Browse files
committed
Merge pull request #643 from tamird/fix-c-warnings
Fix more C warnings
2 parents 5c5543f + 83d36e2 commit bb72702

File tree

8 files changed

+53
-41
lines changed

8 files changed

+53
-41
lines changed

ext/mysql2/client.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ static void *nogvl_close(void *ptr) {
213213
mysql_client_wrapper *wrapper;
214214
wrapper = ptr;
215215
if (wrapper->connected) {
216-
wrapper->active_thread = Qnil;
216+
MARK_CONN_INACTIVE(self);
217217
wrapper->connected = 0;
218218
#ifndef _WIN32
219219
/* Invalidate the socket before calling mysql_close(). This prevents
@@ -257,7 +257,7 @@ static VALUE allocate(VALUE klass) {
257257
mysql_client_wrapper * wrapper;
258258
obj = Data_Make_Struct(klass, mysql_client_wrapper, rb_mysql_client_mark, rb_mysql_client_free, wrapper);
259259
wrapper->encoding = Qnil;
260-
wrapper->active_thread = Qnil;
260+
MARK_CONN_INACTIVE(self);
261261
wrapper->server_version = 0;
262262
wrapper->reconnect_enabled = 0;
263263
wrapper->connect_timeout = 0;
@@ -413,7 +413,7 @@ static VALUE do_send_query(void *args) {
413413
mysql_client_wrapper *wrapper = query_args->wrapper;
414414
if ((VALUE)rb_thread_call_without_gvl(nogvl_send_query, args, RUBY_UBF_IO, 0) == Qfalse) {
415415
/* an error occurred, we're not active anymore */
416-
wrapper->active_thread = Qnil;
416+
MARK_CONN_INACTIVE(self);
417417
return rb_raise_mysql2_error(wrapper);
418418
}
419419
return Qnil;
@@ -444,7 +444,7 @@ static void *nogvl_do_result(void *ptr, char use_result) {
444444

445445
/* once our result is stored off, this connection is
446446
ready for another command to be issued */
447-
wrapper->active_thread = Qnil;
447+
MARK_CONN_INACTIVE(self);
448448

449449
return result;
450450
}
@@ -513,7 +513,7 @@ struct async_query_args {
513513
static VALUE disconnect_and_raise(VALUE self, VALUE error) {
514514
GET_CLIENT(self);
515515

516-
wrapper->active_thread = Qnil;
516+
MARK_CONN_INACTIVE(self);
517517
wrapper->connected = 0;
518518

519519
/* Invalidate the MySQL socket to prevent further communication.
@@ -589,7 +589,7 @@ static VALUE finish_and_mark_inactive(void *args) {
589589
result = (MYSQL_RES *)rb_thread_call_without_gvl(nogvl_store_result, wrapper, RUBY_UBF_IO, 0);
590590
mysql_free_result(result);
591591

592-
wrapper->active_thread = Qnil;
592+
MARK_CONN_INACTIVE(self);
593593
}
594594

595595
return Qnil;
@@ -611,7 +611,6 @@ void rb_mysql_client_set_active_thread(VALUE self) {
611611
const char *thr = StringValueCStr(inspect);
612612

613613
rb_raise(cMysql2Error, "This connection is in use by: %s", thr);
614-
(void)RB_GC_GUARD(inspect);
615614
}
616615
}
617616

@@ -1231,7 +1230,6 @@ void init_mysql2_client() {
12311230
}
12321231
if (lib[i] != MYSQL_LINK_VERSION[i]) {
12331232
rb_raise(rb_eRuntimeError, "Incorrect MySQL client library version! This gem was compiled for %s but the client library is %s.", MYSQL_LINK_VERSION, lib);
1234-
return;
12351233
}
12361234
}
12371235
#endif
@@ -1240,7 +1238,6 @@ void init_mysql2_client() {
12401238
/* without race condition in the library */
12411239
if (mysql_library_init(0, NULL, NULL) != 0) {
12421240
rb_raise(rb_eRuntimeError, "Could not initialize MySQL client library");
1243-
return;
12441241
}
12451242

12461243
#if 0

ext/mysql2/client.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,14 @@ typedef struct {
5959
void rb_mysql_client_set_active_thread(VALUE self);
6060

6161
#define MARK_CONN_INACTIVE(conn) do {\
62-
GET_CLIENT(conn); \
6362
wrapper->active_thread = Qnil; \
6463
} while(0)
6564

6665
#define GET_CLIENT(self) \
6766
mysql_client_wrapper *wrapper; \
6867
Data_Get_Struct(self, mysql_client_wrapper, wrapper);
6968

70-
void init_mysql2_client();
69+
void init_mysql2_client(void);
7170
void decr_mysql2_client(mysql_client_wrapper *wrapper);
7271

7372
#endif

ext/mysql2/extconf.rb

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,43 @@ def asplode(lib)
9393
end
9494

9595
# This is our wishlist. We use whichever flags work on the host.
96-
# TODO: fix statement.c and remove -Wno-declaration-after-statement
97-
# TODO: fix gperf mysql_enc_name_to_ruby.h and remove -Wno-missing-field-initializers
98-
%w(
99-
-Wall
100-
-Wextra
101-
-Werror
102-
-Wno-unused-function
103-
-Wno-declaration-after-statement
104-
-Wno-missing-field-initializers
105-
).each do |flag|
106-
$CFLAGS << ' ' << flag if try_link('int main() {return 0;}', flag)
96+
# -Wall and -Wextra are included by default.
97+
wishlist = [
98+
'-Weverything',
99+
'-Wno-bad-function-cast', # rb_thread_call_without_gvl returns void * that we cast to VALUE
100+
'-Wno-conditional-uninitialized', # false positive in client.c
101+
'-Wno-covered-switch-default', # result.c -- enum_field_types (when fully covered, e.g. mysql 5.5)
102+
'-Wno-declaration-after-statement', # GET_CLIENT followed by GET_STATEMENT in statement.c
103+
'-Wno-disabled-macro-expansion', # rubby :(
104+
'-Wno-documentation-unknown-command', # rubby :(
105+
'-Wno-missing-field-initializers', # gperf generates bad code
106+
'-Wno-missing-variable-declarations', # missing symbols due to ruby native ext initialization
107+
'-Wno-padded', # mysql :(
108+
'-Wno-sign-conversion', # gperf generates bad code
109+
'-Wno-static-in-inline', # gperf generates bad code
110+
'-Wno-switch-enum', # result.c -- enum_field_types (when not fully covered, e.g. mysql 5.6+)
111+
'-Wno-undef', # rubinius :(
112+
'-Wno-used-but-marked-unused', # rubby :(
113+
]
114+
115+
if ENV['CI']
116+
wishlist += [
117+
'-Werror',
118+
'-fsanitize=address',
119+
'-fsanitize=cfi',
120+
'-fsanitize=integer',
121+
'-fsanitize=memory',
122+
'-fsanitize=thread',
123+
'-fsanitize=undefined',
124+
]
107125
end
108126

127+
usable_flags = wishlist.select do |flag|
128+
try_link('int main() {return 0;}', flag)
129+
end
130+
131+
$CFLAGS << ' ' << usable_flags.join(' ')
132+
109133
if RUBY_PLATFORM =~ /mswin|mingw/
110134
# Build libmysql.a interface link library
111135
require 'rake'

ext/mysql2/infile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ mysql2_local_infile_init(void **ptr, const char *filename, void *userdata)
5656
* < 0 error
5757
*/
5858
static int
59-
mysql2_local_infile_read(void *ptr, char *buf, uint buf_len)
59+
mysql2_local_infile_read(void *ptr, char *buf, unsigned int buf_len)
6060
{
6161
int count;
6262
mysql2_local_infile_data *data = (mysql2_local_infile_data *)ptr;
@@ -95,7 +95,7 @@ mysql2_local_infile_end(void *ptr)
9595
* Error message number (see http://dev.mysql.com/doc/refman/5.0/en/error-messages-client.html)
9696
*/
9797
static int
98-
mysql2_local_infile_error(void *ptr, char *error_msg, uint error_msg_len)
98+
mysql2_local_infile_error(void *ptr, char *error_msg, unsigned int error_msg_len)
9999
{
100100
mysql2_local_infile_data *data = (mysql2_local_infile_data *) ptr;
101101

ext/mysql2/mysql2_ext.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
#ifndef MYSQL2_EXT
22
#define MYSQL2_EXT
33

4+
void Init_mysql2(void);
5+
46
/* tell rbx not to use it's caching compat layer
57
by doing this we're making a promise to RBX that
68
we'll never modify the pointers we get back from RSTRING_PTR */
79
#define RSTRING_NOT_MODIFIED
810
#include <ruby.h>
911

10-
#ifndef HAVE_UINT
11-
#define HAVE_UINT
12-
typedef unsigned short ushort;
13-
typedef unsigned int uint;
14-
#endif
15-
1612
#ifdef HAVE_MYSQL_H
1713
#include <mysql.h>
1814
#include <mysql_com.h>

ext/mysql2/result.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#include <mysql2_ext.h>
22

3-
#include <stdint.h>
4-
5-
#ifdef HAVE_RUBY_ENCODING_H
63
#include "mysql_enc_to_ruby.h"
74

5+
#ifdef HAVE_RUBY_ENCODING_H
86
static rb_encoding *binaryEncoding;
97
#endif
108

@@ -161,8 +159,7 @@ static void *nogvl_stmt_fetch(void *ptr) {
161159
return (void *)r;
162160
}
163161

164-
165-
static VALUE rb_mysql_result_fetch_field(VALUE self, unsigned int idx, short int symbolize_keys) {
162+
static VALUE rb_mysql_result_fetch_field(VALUE self, unsigned int idx, int symbolize_keys) {
166163
VALUE rb_field;
167164
GET_RESULT(self);
168165

@@ -502,7 +499,6 @@ static VALUE rb_mysql_result_fetch_row_stmt(VALUE self, MYSQL_FIELD * fields, co
502499
default:
503500
rb_raise(cMysql2Error, "unhandled buffer type: %d",
504501
result_buffer->buffer_type);
505-
break;
506502
}
507503
}
508504

@@ -756,7 +752,7 @@ static VALUE rb_mysql_result_fetch_fields(VALUE self) {
756752
wrapper->fields = rb_ary_new2(wrapper->numberOfFields);
757753
}
758754

759-
if ((unsigned)RARRAY_LEN(wrapper->fields) != wrapper->numberOfFields) {
755+
if ((my_ulonglong)RARRAY_LEN(wrapper->fields) != wrapper->numberOfFields) {
760756
for (i=0; i<wrapper->numberOfFields; i++) {
761757
rb_mysql_result_fetch_field(self, i, symbolizeKeys);
762758
}

ext/mysql2/result.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef MYSQL2_RESULT_H
22
#define MYSQL2_RESULT_H
33

4-
void init_mysql2_result();
4+
void init_mysql2_result(void);
55
VALUE rb_mysql_result_to_obj(VALUE client, VALUE encoding, VALUE options, MYSQL_RES *r, VALUE statement);
66

77
typedef struct {
@@ -10,8 +10,8 @@ typedef struct {
1010
VALUE client;
1111
VALUE encoding;
1212
VALUE statement;
13-
unsigned int numberOfFields;
14-
unsigned long numberOfRows;
13+
my_ulonglong numberOfFields;
14+
my_ulonglong numberOfRows;
1515
unsigned long lastRowProcessed;
1616
char is_streaming;
1717
char streamingComplete;

ext/mysql2/statement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ typedef struct {
99
int refcount;
1010
} mysql_stmt_wrapper;
1111

12-
void init_mysql2_statement();
12+
void init_mysql2_statement(void);
1313
void decr_mysql2_stmt(mysql_stmt_wrapper *stmt_wrapper);
1414

1515
VALUE rb_mysql_stmt_new(VALUE rb_client, VALUE sql);

0 commit comments

Comments
 (0)