Issue #13830 has been updated by nagachika (Tomoyuki Chikanaga).

Backport changed from 2.2: DONTNEED, 2.3: DONTNEED, 2.4: REQUIRED to 2.2: DONTNEED, 2.3: DONTNEED, 2.4: DONE

ruby_2_4 r59811 merged revision(s) 59624,59626.

----------------------------------------
Bug #13830: `rb_scan_args`'s result differs based on optimization level
https://bugs.ruby-lang.org/issues/13830#change-66573

* Author: graywolf (Gray Wolf)
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.5.0dev (2017-08-19 trunk 59623) [x86_64-linux]
* Backport: 2.2: DONTNEED, 2.3: DONTNEED, 2.4: DONE
----------------------------------------
I think I've found bug in `rb_scan_args` function. This is my testing script:

    require_relative 'test'

    Test.test "server", "host", "a", "b", license: "lic"

And this is implementation of my module:

    #include <ruby.h>

    VALUE module = Qnil;

    VALUE method_test(int argc, VALUE * argv, VALUE self);

    void Init_test(void) {
        module = rb_define_module("Test");
        rb_define_singleton_method(module, "test", method_test, -1);
    }

    VALUE method_test(int argc, VALUE * argv, VALUE self) {
        VALUE name, host, others, hash;
        name = Qnil;
        host = Qnil;
        others = Qnil;
        hash = Qnil;

        rb_scan_args(argc, argv, "11*:", &name, &host, &others, &hash);

        rb_p(name);
        rb_p(host);
        rb_p(others);
        rb_p(hash);

        return Qnil;
    }

However, when I run it the result differs based on optimization level.

`-O0`:

    $ ~/ruby_install_O0/bin/ruby do_test.rb
    "server"
    "host"
    ["a", "b"]
    {:license=>"lic"}

`-O1`:

    "server"
    "host"
    ["a", "b", {:license=>"lic"}]
    nil

`-O2`:

    "server"
    "host"
    ["a", "b", {:license=>"lic"}]
    nil

`-O3`:

    "server"
    "host"
    ["a", "b", {:license=>"lic"}]
    nil

Configure outputs:

`-O0`:

    ---
    Configuration summary for ruby version 2.5.0

       * Installation prefix: /home/wolf/ruby_install_O0
       * exec prefix:         ${prefix}
       * arch:                x86_64-linux
       * site arch:           ${arch}
       * RUBY_BASE_NAME:      ruby
       * ruby lib prefix:     ${libdir}/${RUBY_BASE_NAME}
       * site libraries path: ${rubylibprefix}/${sitearch}
       * vendor path:         ${rubylibprefix}/vendor_ruby
       * target OS:           linux
       * compiler:            gcc
       * with pthread:        yes
       * enable shared libs:  no
       * dynamic library ext: so
       * CFLAGS:              ${optflags} ${debugflags} ${warnflags}
       * LDFLAGS:             -L. -fstack-protector -rdynamic \
                              -Wl,-export-dynamic
       * optflags:            -O0 -fno-fast-math
       * debugflags:          -ggdb3 -DRUBY_DEVEL=1
       * warnflags:           -Wall -Wextra -Wno-unused-parameter \
                              -Wno-parentheses -Wno-long-long \
                              -Wno-missing-field-initializers \
                              -Wno-tautological-compare \
                              -Wno-parentheses-equality \
                              -Wno-constant-logical-operand -Wno-self-assign \
                              -Wunused-variable -Werror=implicit-int \
                              -Werror=pointer-arith -Werror=write-strings \
                              -Werror=declaration-after-statement \
                              -Werror=implicit-function-declaration \
                              -Werror=deprecated-declarations \
                              -Wno-packed-bitfield-compat \
                              -Wsuggest-attribute=noreturn \
                              -Wsuggest-attribute=format \
                              -Wimplicit-fallthrough=0
       * strip command:       strip -S -x
       * install doc:         yes
       * man page type:       doc

    ---

`-O1`:

    ---
    Configuration summary for ruby version 2.5.0

       * Installation prefix: /home/wolf/ruby_install_O1
       * exec prefix:         ${prefix}
       * arch:                x86_64-linux
       * site arch:           ${arch}
       * RUBY_BASE_NAME:      ruby
       * ruby lib prefix:     ${libdir}/${RUBY_BASE_NAME}
       * site libraries path: ${rubylibprefix}/${sitearch}
       * vendor path:         ${rubylibprefix}/vendor_ruby
       * target OS:           linux
       * compiler:            gcc
       * with pthread:        yes
       * enable shared libs:  no
       * dynamic library ext: so
       * CFLAGS:              ${optflags} ${debugflags} ${warnflags}
       * LDFLAGS:             -L. -fstack-protector -rdynamic \
                              -Wl,-export-dynamic
       * optflags:            -O1 -fno-fast-math
       * debugflags:          -ggdb3 -DRUBY_DEVEL=1
       * warnflags:           -Wall -Wextra -Wno-unused-parameter \
                              -Wno-parentheses -Wno-long-long \
                              -Wno-missing-field-initializers \
                              -Wno-tautological-compare \
                              -Wno-parentheses-equality \
                              -Wno-constant-logical-operand -Wno-self-assign \
                              -Wunused-variable -Werror=implicit-int \
                              -Werror=pointer-arith -Werror=write-strings \
                              -Werror=declaration-after-statement \
                              -Werror=implicit-function-declaration \
                              -Werror=deprecated-declarations \
                              -Wno-packed-bitfield-compat \
                              -Wsuggest-attribute=noreturn \
                              -Wsuggest-attribute=format \
                              -Wimplicit-fallthrough=0
       * strip command:       strip -S -x
       * install doc:         yes
       * man page type:       doc

    ---

I think the behaviour under `-O0` is the correct one?



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>