Menu

#1136 Strange behaviour on method names containing underscores (_)

None
closed-fixed
ruby (61)
5
2022-02-02
2011-02-27
No

This is a clarification for Bug 3086615 which has been prematurely closed. ( https://sourceforge.net/tracker/?func=detail&aid=3086615&group_id=1645&atid=101645 )

In that report, I forgot to mention one precondition for this issue: The class has to be named according to the stripped prefix. So the following "example.i" triggers the bug:

%module example
%inline %{
class Cool {
public:
int somethingCool_Fast() {
return 42;
}
};
%}

The "example.so" can be built with the following commands:

swig -ruby -c++ example.i
g++ -shared -fPIC -I"`ruby -r rbconfig -e 'puts(Config::CONFIG["archdir"] || "")'`" -o example.so example_wrap.cxx

Then, the following Ruby snippet fails:

require 'example'
c = Example::Cool.new
puts c.somethingCool_Fast() # fails, but shouldn't

However, the following Ruby code works, although it shouldn't:

require 'example'
c = Example::Cool.new
puts c.somethingFast() # outputs "42", but should fail as there is no such method

I confirmed that this issue still exists in the current SWIG 2.0.2 release.

Discussion

  • William Fulton

    William Fulton - 2012-12-10
    • assigned_to: gga73 --> kkaempf
     
  • Olly Betts

    Olly Betts - 2022-02-02

    Still present in git master.

    This patch seems to fix it, and make check-ruby-test-suite still passes:

    --- a/Source/Modules/ruby.cxx
    +++ b/Source/Modules/ruby.cxx
    @@ -108,7 +108,7 @@ public:
         Clear(temp);
         Append(temp, s);
         if (Strncmp(s, prefix, Len(prefix)) == 0) {
    
    -      Replaceall(temp, prefix, "");
    +      Replace(temp, prefix, "", DOH_REPLACE_FIRST);
         }
         return Char(temp);
       }
    

    Actually it should really just erase (or not copy in the first place) the first Len(prefix) characters given we do the Strncmp() and so know the string starts with prefix.

    Needs the testcase adding to the testsuite too.

     
  • Olly Betts

    Olly Betts - 2022-02-02

    Fixed in git master for 4.1.0:

    commit 2e981895642a778ef96adf0396e7d5d1965ac03f (HEAD -> master, origin/master, origin/HEAD)
    Author: Olly Betts <olly@survex.com>
    Date:   Wed Feb 2 15:53:23 2022 +1300
    
        [Ruby] Fix remove of prefix from method name
    
        The prefix is now only removed at the start.
    
        Fixes https://sourceforge.net/p/swig/bugs/1136/
    
     
  • Olly Betts

    Olly Betts - 2022-02-02
    • status: open --> closed-fixed
    • Group: -->
     

Log in to post a comment.

MongoDB Logo MongoDB