Commit Graph

54 Commits

Author SHA1 Message Date
William S Fulton de573cda78 Pass SWIG_FromCharPtrAndSize and friends on to the C compiler
A bare #define at the start of a line in a brace delimited %fragment body is
consumed by the SWIG preprocessor and never reaches the generated wrapper. The
macro therefore only exists at SWIG level and code that SWIG does not macro
expand, such as a typemap delimited with %{ ... %}, is left referring to an
undefined name. Use %#define so the definition is emitted for the C compiler.

Fixes SWIG_FromCharPtrAndSize for Python, SWIG_FromBinaryCharPtrAndSize for
Octave, Perl, R and Ruby, SWIG_ToUint8Array and SWIG_FromUint8Array for
JavaScript, and the SWIG_AsVal_* and SWIG_From_* macros in the Scilab char,
short, signed char, unsigned char, unsigned long and long long files.

Lib/scilab/scipointer.swg is deliberately left alone. Its fragments are never
requested by any typemap, so they are only emitted as SWIG preprocessor
definitions; switching them to %#define drops SWIG_ConvertPtr and
SWIG_NewPointerObj from the generated code entirely. Fixing those needs the
definitions moving to a runtime insert, as the other target languages do.

Add a common test case charptr_fragment. It uses a %{ ... %} delimited typemap
so the macro name survives into the wrapper, which only compiles when the
library defines the macro for the C compiler. The target languages without
these macros wrap the same functions using their default typemaps. Verified
that the generated Python module fails to load before this change with an
undefined symbol for SWIG_FromCharPtrAndSize.

Follows on from #3522.

Assisted-by: Claude Code (Opus 5)
2026-07-31 00:10:40 +01:00
Julien Schueller f505f2566f Python: Drop PY_VERSION_HEX<3.5 code
Closes #3201
2026-07-06 23:50:30 +01:00
William S Fulton 0c291fa448 Fix one-byte buffer overflow in cdata.i memmove
The cdata.i (const void *BYTES, size_t LENGTH) typemap took LENGTH from
SWIG_AsCharPtrAndSize, whose psize includes a trailing NUL (data length
+ 1). When the destination buffer is sized exactly to the data, memmove
wrote one byte past the end. Confirmed with valgrind: "Invalid write of
size 1, 0 bytes after a block of size 512".

- Make SWIG_AsCharPtrAndSize binary aware in the languages that use the
  generic Lib/cdata.i (Python, Perl, Ruby, Octave, R): when the caller
  passes SWIG_BINARYSTR it now reports the exact byte count, not data
  + 1. Non cdata callers are unaffected as they never set the flag.
- Lua li_cdata_bytes tests looped to 0x99 instead of 0xff, verifying
  only 154 of the 256 byte values; corrected to 0xff.
- Tcl cdata.i passed an int * to Tcl_GetSizeIntFromObj, which expects
  a Tcl_Size * on Tcl 8.7 and 9; use Tcl_Size and reject values that
  are not a byte.
- CHANGES.current: document the cdata type change and the overflow fix.

Assisted-by: Claude Opus 4.7
2026-05-25 13:17:15 +01:00
William S Fulton da33dfe05b Rename bin abbreviation to binary in cdata identifiers
Use the full English word in the identifiers added by the previous
commit for cdata raw byte handling:

  SWIG_BINSTR                 -> SWIG_BINARYSTR
  SWIG_BINSTRMASK             -> SWIG_BINARYSTRMASK
  SWIG_AddBinMask             -> SWIG_AddBinaryStrMask
  SWIG_DelBinMask             -> SWIG_DelBinaryStrMask
  SWIG_IsBinStr               -> SWIG_IsBinaryStr
  SWIG_FromBinCharPtrAndSize  -> SWIG_FromBinaryCharPtrAndSize
  useBin (local variable)     -> use_binary

Touches the constants in Lib/swigrun.swg, the SWIG_FromBinary*
macro defines in pystrings.swg, perlstrings.swg, rubystrings.swg,
octprimtypes.swg and rfragments.swg, and all call sites in
Lib/cdata.i and Lib/python/pystrings.swg.

Assisted-by: Claude Opus 4.7
2026-05-25 13:16:09 +01:00
Erez Geva cd569d122a cdata.i changes and new test li_cdata_bytes
Fix guile cdata.

Add SWIG_BINSTR flag
- Add SWIG_FromBinCharPtrAndSize with the new flag
  To add languages that use Lib/cdata.i.
- python use binary string by using the new SWIG_BINSTR
  in SWIG_AsCharPtrAndSize and SWIG_FromBinCharPtrAndSize.

Languages that were changed to uses list
- Tcl use list of integers.
- scilab use list of uint8.
  And support passing list of numbers to C.

Add support to JavaScript
- use Uint8Array instead of strings.
- Add li_cdata_carrays. tests.
- Add li_cdata_bytes tests.
- Use Debian node-addon-api package location.
- napi folder location to Examples/test-suite/javascript/Makefile.in.
- Update documentation.

Update cdata.i documentation

This new cdata test focus on:
- Ensure we can receive proper data from C and pass proper data back to C.
- Use all possoble byte values , i.e. the full range of 0 to 255
  and ensure values 128 to 255 do not pass Unicode transformation (UTF-8/16).
- Ensure zero is a valid value and not a string null termination
  nor a modified UTF-8 which transform U+0000 to 0xC0 0x80.
- Check mutability of the cdata object.

Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
2026-05-25 13:16:09 +01:00
William S Fulton a1e619b7cb argcargv / OOM-protection follow-ups
- [Lua] Fix off-by-one when nil terminates the table early: use $1 = i
  (not i + 1) to match the count of strings actually copied, and pop
  the nil from the Lua stack before breaking.

- [Python] In SWIG_AsCharPtrAndSize (pystrings.swg) decref bytes / obj
  on the SWIG_MemoryError early returns added by the previous commits,
  so an allocation failure no longer leaks the temporary PyObject.

- [Octave, Python] In argcargv.i free already-allocated string entries
  and the array itself before failing on a per-element OOM (Python uses
  the existing break path so the freearg typemap performs the cleanup).

- [Perl, PHP, Tcl] Drop unreachable `goto fail` after SWIG_croak /
  SWIG_PHP_Error / SWIG_exception_fail, all of which already invoke
  SWIG_fail. Compilers warning on unreachable code complained.

- Standardise the OOM error wording across languages and fix the
  3- and 5-space indentation introduced by the previous commits.

- Update Examples/perl5/xmlstring/xmlstring.i to the new
  %typemaps_string signature (it picks up the missing WarningLeakMsg
  argument too) and add a SWIG_NewCopyXMLChArray fragment.

- CHANGES.current: document the API breakages introduced by removing
  %new_copy_array, removing %typemaps_string_alloc and extending
  %typemaps_string, including the actual error text users will see.

Assisted-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-19 21:01:30 +01:00
Erez Geva 5107439255 Replace `%new_copy_array` with protected allocation
Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
2026-05-19 21:01:30 +01:00
Jonatan Wallmander 51430f7dd9
Python: Fix warnings about implicit type conversions (#3287)
* Python: Fix warnings about implicit type conversions

When compiling with -Wconversion -Wsign-conversion on
gcc, these places resulted in warnings.

Add explicit type casts to be clear to the compiler.

* new_copy_array casts size to size_t

This alleviates warnings when -Wconversion -Wsign-conversion
are enabled.

* Fix various conversions to correct integer signedness

len can be signed as is the case with python's size_t.
This casts it to size_t to avoid compilation warnings.

* Add -Wconversion and -Wsign-conversion compilation flags for tests
2025-12-05 07:30:05 +00:00
William S Fulton 0c6c9fd476 Cosmetic whitespace formatting around PY_VERSION_HEX 2025-06-02 19:06:43 +01:00
William S Fulton 54596b44f1 Fix maximum size of strings being marshalled from C/C++ into Python
This has been changed from INT_MAX to PY_SSIZE_T_MAX in line
with the Python C API maximum string length handling change in python-2.5.

Closes #3134
2025-04-30 19:07:36 +01:00
William S Fulton 67bc1cbb0f Py_XDECREF -> Py_DecRef for stable ABI compliance 2024-03-24 18:12:21 +00:00
William S Fulton 2e956c1607 SWIG_AsCharPtrAndSize simplification and performance improvement
Only make a copy of the returned string if the passed in Python object
does not own the returned char* string. Callers of SWIG_AsCharPtrAndSize
should now ensure that the returned string is only used while the passed
in PyObject * string is valid. Performance improvement is for Python 3
input string handling.

The new SWIG_PyUnicode_AsUTF8AndSize function allows for this simplification.
2023-12-15 22:31:50 +00:00
William S Fulton 2808e1d834 Simpler SWIG_AsCharPtrAndSize implementation - remove usage of SWIG_Python_str_AsChar 2023-12-13 08:30:22 +00:00
Zackery Spytz 6adf19b52f Check Py{Bytes,String}_AsStringAndSize() for failure
PyBytes_AsStringAndSize() and PyString_AsStringAndSize() were not
being checked for failure.

Closes #1349.
2020-01-09 21:14:12 -07:00
William S Fulton de26ebbad1 Remove Python -safecstrings option.
This option, if used, has not had any effect on Python 3 code since commit a863d3 9 years ago.
I think we can assume that it is not needed for Python 3.
Running the examples and test-suite (Python 2) doesn't change the code
paths with and without -safecstrings because only SWIG_OLDOBJ and SWIG_NEWOBJ
are used in the typemaps and the following code is thus unaltered by -safecstrings
(which sets SWIG_PYTHON_SAFE_CSTRINGS):

%#if defined(SWIG_PYTHON_SAFE_CSTRINGS)
  if (*alloc != SWIG_OLDOBJ)
%#else
  if (*alloc == SWIG_NEWOBJ)
%#endif
  {
    *cptr = %new_copy_array(cstr, len + 1, char);
    *alloc = SWIG_NEWOBJ;
printf("safe strings: %s\n", *cptr ? *cptr : "NULLSTRING");
  } else {
    *cptr = cstr;
    *alloc = SWIG_OLDOBJ;
  }

Note: nosafecstrings was also the default and -O didn't actually change this.
2018-10-22 08:19:50 +01:00
Olly Betts a82c1b943b Drop support for Python 3.0 to 3.3
See #701.
2018-06-08 11:08:31 +12:00
William S Fulton b0e29fbdf3 Add missing checks for failures in calls to PyUnicode_AsUTF8String.
Previously a seg fault could occur when passing invalid UTF8 strings (low
surrogates), eg passing u"\udcff" to the C layer (Python 3).
2017-12-04 20:14:04 +00:00
Alec Cooper 33edfa4a62 Add support for Python Bytes/Unicode distinction 2016-02-04 20:16:41 -05:00
William S Fulton e07938ae8e pystrings.swg cosmetic formatting 2015-12-19 04:11:48 +00:00
William S Fulton 291186cfaf Refine Python 2 unicode strings patch 2015-12-19 03:46:44 +00:00
Brian Cole edd36b28d2 Automatically coerce python 2.x unicode objects into UTF8 when passing to underlying code. 2015-12-19 03:44:27 +00:00
William S Fulton c5322a9ecb Python use Py_ssize_t instead of int for better portability 2015-12-05 09:00:04 +00:00
Harvey Falcic 9846f3f1ea Python 3 byte string output: use errors="surrogateescape"
... if available on the version of Python that's in use. This allows
obtaining the original byte string (and potentially trying a fallback
encoding) if the bytes can't be decoded as UTF-8.

Previously, a UnicodeDecodeError would be raised with no way to treat
the data as bytes or try another codec.
2014-05-23 15:40:01 -04:00
Stefan Zager 3d444101d1 A slew of changes based on William Fulton's code review.
- Fixed naming conventions; SwigPyBuiltin is used a lot
- Removed use of std::vector
- builtin.swg isn't included if -builtin isn't specified
- Changed many feature names to use a "python:" prefix
- Eliminated static vars in std_pair.i
- Eliminated C++-style comments (//)
- Enabled autodoc and docstring with -builtin
- Fixed non-ansi generated C code
- Detect and complain if two incompatible swig modules are loaded
- Removed argcargvtest_runme3.py, and fixed argcargvtest_runme.py
  so that 2to3 handles it better
- Removed anonymous namespaces
- Eliminated builtin_init typemaps; consolidated functionality into
  SWIG_Python_NewPointerObj
- Eliminate printf warnings from %U conversion character by switching
  to %S, which works just as well
- Fixed li_std_set_runme.py for python3, which returns set members in
  a different order from python2





git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/szager-python-builtin@12562 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2011-03-29 06:57:02 +00:00
Haoyu Bai a863d31e81 Fix the memory leak related to Python 3 unicode and char * conversion.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@11160 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2009-03-19 15:26:57 +00:00
Haoyu Bai 3d8ddfc442 Merged the Python 3.0 support branch. The merging progress is not so smooth, so hope this commit won't make anything broken.
This is the (incomplemete) log produced by svnmerge.py:

Merged revisions 10405-10409,10420-10422,10426,10438,10445,10451,10454-10465,10467,10473-10475,10485,10488-10489,10493-10495,10497,10509-10510,10513-10514,10517,10520,10525,10528-10529,10533-10535,10554-10557,10570,10573,10593,10614,10666-10669,10673,10678,10687,10690,10704-10706,10731,10744,10750-10752,10755,10759,10770,10775-10776,10813,10819 via svnmerge from 
https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-bhy



git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10834 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2008-09-11 17:18:07 +00:00
William S Fulton d6d23465e0 Patch from Michal Marek for Python 2.5 to fix 64 bit array indexes
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@9289 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2006-09-16 09:57:26 +00:00
Marcelo Matus a2c99f619d reduce the aliasing warnings (when compiling with CFLAGS='-O2 -Wstrict-aliasing=2') to the minimum, ie, enums and pointer to functions
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@8883 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2006-02-23 21:43:33 +00:00
Marcelo Matus 268cc4fb97 add -proxydel/-noproxydel plus minor improvements
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7972 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2005-12-11 10:44:57 +00:00
Marcelo Matus abbf8b406a more on the TypeQuery fix
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7971 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2005-12-10 23:53:53 +00:00
Marcelo Matus ae761ca3ba fix extra calls to TypeQuery for 'char *' and 'wchar_t *'
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7968 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2005-12-10 20:20:19 +00:00
Marcelo Matus 3b3d3118f1 add missing %s
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7964 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2005-12-10 00:40:42 +00:00
Marcelo Matus e0eea5f625 add safecstrings option and leave the default of string conversions as in older version, ie, return the pointer not a copy
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7963 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2005-12-10 00:36:12 +00:00
Marcelo Matus 922d58ac19 don't use Py_None directly nor &PyType_Type
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7925 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2005-12-04 08:50:00 +00:00
Marcelo Matus f3c24eff33 fixes for directors + pointers
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7860 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2005-11-21 21:01:35 +00:00
Marcelo Matus 3c65cea431 Perl added to the Unified typemap library, cleaner way to use the library, and 'normalized' macro names
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7707 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2005-10-24 14:59:05 +00:00
Marcelo Matus aadff06f45 more typemaps unification and fixes for valgrind
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7684 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2005-10-19 10:52:50 +00:00
Marcelo Matus 7e5e4fd1f9 massive typemap unification
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7676 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2005-10-18 13:24:15 +00:00
Marcelo Matus a2f5af9ddd fix #1188207 and uniform the error/null/none handling
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7225 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2005-05-26 20:43:33 +00:00
Marcelo Matus 192e00615d More fix for warnings, and add some of the ideas of the Bill's 'labels' patch
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@6998 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2005-02-24 23:48:20 +00:00
Marcelo Matus 988db075cb fix char* typemap in bug #1080514
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@6856 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2004-12-11 08:30:40 +00:00
Marcelo Matus d809efcb2f delete unused line
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@6494 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2004-10-24 06:30:40 +00:00
Marcelo Matus dc4409a1f1 isolate language independent STD/STL/C++ code + more documentation + cleaning
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@6382 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2004-10-10 06:42:15 +00:00
Marcelo Matus e46c80edf8 replacing SWIGSTATIC(a) by SWIGINTERN a
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@6290 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2004-10-02 20:15:46 +00:00
Marcelo Matus a4a7a2ff1f better error messages, more comments, clean up for the next major modifications
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@6284 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2004-10-02 01:43:17 +00:00
Marcelo Matus 349e6bc6ee using <> when including lib files, fix char{ANY], add char FIXSIZE[ANY], fix pyfragments.swg, fixing *.swg and *.i names
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@6250 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2004-09-17 06:17:35 +00:00
Marcelo Matus bc715fc9a0 fix warnings for intel compiler
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@6194 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2004-09-01 08:07:45 +00:00
Marcelo Matus 12af360d6f fixes for char[], const char[], and cleaning wchar_t
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@5930 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2004-05-27 11:25:31 +00:00
Marcelo Matus 459e527647 add wstring/wchar_t support
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@5829 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2004-04-05 20:12:02 +00:00
Marcelo Matus f896efa6dd fixes for strings and cosmetics
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@5792 626c5289-ae23-0410-ae9c-e8d60b6d4f22
2004-03-28 13:34:16 +00:00