diff options
author | Olly Betts <olly@survex.com> | 2023-04-23 12:10:21 +1200 |
---|---|---|
committer | Olly Betts <olly@survex.com> | 2023-04-27 10:08:57 +1200 |
commit | 1f5ff2e6a585d0b5dca501945fabfeb48b1bf547 (patch) | |
tree | 8e89ad3733384a7dc952b74a8243562bee3cb85e /Lib/ruby | |
parent | 589373309dae750ed05a46b1b955993da253a287 (diff) | |
download | swig-1f5ff2e6a585d0b5dca501945fabfeb48b1bf547.tar.gz |
Allow using snprintf() instead of sprintf() in wrappers
We aim to produce code that works with C90 or C++98 so we can't
assume snprintf() is available, but it almost always is (even
on systems from before it was standardised) so having a way to
use it is helpful.
Enable this automatically if the compiler claims conformance
with at least C90 or C++98 and check SWIG_HAVE_SNPRINTF to allow
turning on manually, but disable if SWIG_NO_SNPRINTF if defined.
The fallback is to call sprintf() without a buffer size check -
checking after the call is really shutting the stable door after
the horse has bolted, and most of our uses either have a fixed maximum
possible size or dynamically allocate a buffer that's large enough.
Fixes: #2502 (sprintf deprecation warnings on macos)
Fixes: #2548
Diffstat (limited to 'Lib/ruby')
-rw-r--r-- | Lib/ruby/rubycontainer.swg | 2 | ||||
-rw-r--r-- | Lib/ruby/rubyerrors.swg | 2 | ||||
-rw-r--r-- | Lib/ruby/rubyrun.swg | 10 |
3 files changed, 8 insertions, 6 deletions
diff --git a/Lib/ruby/rubycontainer.swg b/Lib/ruby/rubycontainer.swg index 597ae83d2..ab2eeae83 100644 --- a/Lib/ruby/rubycontainer.swg +++ b/Lib/ruby/rubycontainer.swg @@ -186,7 +186,7 @@ namespace swig return swig::as<T>(item); } catch (const std::invalid_argument& e) { char msg[1024]; - sprintf(msg, "in sequence element %d ", _index); + SWIG_snprintf(msg, sizeof(msg), "in sequence element %d ", _index); VALUE lastErr = rb_gv_get("$!"); if ( lastErr == Qnil ) { %type_error(swig::type_name<T>()); diff --git a/Lib/ruby/rubyerrors.swg b/Lib/ruby/rubyerrors.swg index 434544bc9..ad71d1d39 100644 --- a/Lib/ruby/rubyerrors.swg +++ b/Lib/ruby/rubyerrors.swg @@ -110,7 +110,7 @@ const char* Ruby_Format_TypeError( const char* msg, } str = rb_str_cat2( str, "Expected argument " ); - sprintf( buf, "%d of type ", argn-1 ); + SWIG_snprintf( buf, sizeof( buf), "%d of type ", argn-1 ); str = rb_str_cat2( str, buf ); str = rb_str_cat2( str, type ); str = rb_str_cat2( str, ", but got " ); diff --git a/Lib/ruby/rubyrun.swg b/Lib/ruby/rubyrun.swg index 885292481..456bd9d8f 100644 --- a/Lib/ruby/rubyrun.swg +++ b/Lib/ruby/rubyrun.swg @@ -151,8 +151,9 @@ SWIG_Ruby_InitRuntime(void) SWIGRUNTIME void SWIG_Ruby_define_class(swig_type_info *type) { - char *klass_name = (char *) malloc(4 + strlen(type->name) + 1); - sprintf(klass_name, "TYPE%s", type->name); + size_t klass_len = 4 + strlen(type->name) + 1; + char *klass_name = (char *) malloc(klass_len); + SWIG_snprintf(klass_name, klass_len, "TYPE%s", type->name); if (NIL_P(_cSWIG_Pointer)) { _cSWIG_Pointer = rb_define_class_under(_mSWIG, "Pointer", rb_cObject); rb_undef_method(CLASS_OF(_cSWIG_Pointer), "new"); @@ -208,8 +209,9 @@ SWIG_Ruby_NewPointerObj(void *ptr, swig_type_info *type, int flags) SWIG_RubyAddTracking(ptr, obj); } } else { - klass_name = (char *) malloc(4 + strlen(type->name) + 1); - sprintf(klass_name, "TYPE%s", type->name); + size_t klass_len = 4 + strlen(type->name) + 1; + klass_name = (char *) malloc(klass_len); + SWIG_snprintf(klass_name, klass_len, "TYPE%s", type->name); klass = rb_const_get(_mSWIG, rb_intern(klass_name)); free((void *) klass_name); obj = Data_Wrap_Struct(klass, 0, 0, ptr); |