Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fixed size arrays in case the compiler doesn't support C99 variable ... #7

Merged
merged 1 commit into from
Mar 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ext/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
have_header 'unistd.h'
have_header 'ruby/st.h' or have_header 'st.h' or abort "pg currently requires the ruby/st.h header"

checking_for "C99 variable length arrays" do
$defs.push( "-DHAVE_VARIABLE_LENGTH_ARRAYS" ) if try_compile('void test_vla(int l){ int vla[l]; }')
end

create_header()
create_makefile( "pg_ext" )

9 changes: 9 additions & 0 deletions ext/pg.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@
typedef long suseconds_t;
#endif

#if defined(HAVE_VARIABLE_LENGTH_ARRAYS)
#define PG_VARIABLE_LENGTH_ARRAY(type, name, len, maxlen) type name[(len)];
#else
#define PG_VARIABLE_LENGTH_ARRAY(type, name, len, maxlen) \
type name[(maxlen)] = {(len)>(maxlen) ? (rb_raise(rb_eArgError, "Number of " #name " (%d) exceeds allowed maximum of " #maxlen, (len) ), (type)1) : (type)0};

#define PG_MAX_COLUMNS 4000
#endif

/* The data behind each PG::Connection object */
typedef struct {
PGconn *pgconn;
Expand Down
6 changes: 3 additions & 3 deletions ext/pg_result.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ pgresult_each_row(VALUE self)
num_fields = PQnfields(this->pgresult);

for ( row = 0; row < num_rows; row++ ) {
VALUE row_values[num_fields];
PG_VARIABLE_LENGTH_ARRAY(VALUE, row_values, num_fields, PG_MAX_COLUMNS)

/* populate the row */
for ( field = 0; field < num_fields; field++ ) {
Expand Down Expand Up @@ -892,7 +892,7 @@ pgresult_values(VALUE self)
VALUE results = rb_ary_new2( num_rows );

for ( row = 0; row < num_rows; row++ ) {
VALUE row_values[num_fields];
PG_VARIABLE_LENGTH_ARRAY(VALUE, row_values, num_fields, PG_MAX_COLUMNS)

/* populate the row */
for ( field = 0; field < num_fields; field++ ) {
Expand Down Expand Up @@ -1176,7 +1176,7 @@ pgresult_stream_each_row(VALUE self)
}

for ( row = 0; row < ntuples; row++ ) {
VALUE row_values[nfields];
PG_VARIABLE_LENGTH_ARRAY(VALUE, row_values, nfields, PG_MAX_COLUMNS)
int field;

/* populate the row */
Expand Down
2 changes: 1 addition & 1 deletion ext/pg_text_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ pg_text_dec_identifier(t_pg_coder *conv, char *val, int len, int tuple, int fiel
int word_index = 0;
int index;
/* Use a buffer of the same length, as that will be the worst case */
char word[len + 1];
PG_VARIABLE_LENGTH_ARRAY(char, word, len + 1, NAMEDATALEN)

/* The current character in the input string. */
char c;
Expand Down
2 changes: 1 addition & 1 deletion ext/pg_type_map_by_mri_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static VALUE rb_cTypeMapByMriType;
typedef struct {
t_typemap typemap;
struct pg_tmbmt_converter {
FOR_EACH_MRI_TYPE( DECLARE_CODER );
FOR_EACH_MRI_TYPE( DECLARE_CODER )
} coders;
} t_tmbmt;

Expand Down