Menu

#79 Warn when creating functions that clash with PHP built-ins

None
closed
nobody
None
5
2021-05-13
2011-01-31
Paul Colby
No

Consider the following example:

%module example

%{
class session {
public:
void commit() { };
};
%}

class session {
public:
void commit() { };
};

SWIG will run cleanly on the above example (ie report no errors or warnings). And the resulting *_wrap.cpp will also compile cleanly.

However, once the module is registered with PHP, php-cli will crash:

$ php
PHP Warning: Function registration failed - duplicate name - session_commit in Unknown on line 0
PHP Warning: example: Unable to register functions, unable to load in Unknown on line 0
Segmentation fault
$

The cause of the problem is simple - the SWIG-generated session::commit method wrapper function will be named session_commit, but this function name clashes with PHP's built-in session_commit function (http://php.net/manual/en/function.session-commit.php). Of course, this is easy to fix (see below), but might be quite confusing behavior for some, so it would be nice if SWIG could recognise this situation and warn the user appropriately :) But that might require SWIG to maintain some sort of dictionary of PHP built-in names, which is probably not practical :|

The above example can be fixed by renaming the commit method, such as:

%rename(_commit_) commit();

Discussion

  • Olly Betts

    Olly Betts - 2021-05-13
    • status: open --> closed
    • Group: -->
     
  • Olly Betts

    Olly Betts - 2021-05-13

    This was finally fixed on git master by 502e7185 - we no longer generate these flat function wrappers, but instead directly create classes and methods via PHP's C API. The example now works as-is.

    This fix should be in SWIG 4.1.0.

     

Log in to post a comment.