ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:90481] [Ruby trunk Bug#15409] OpenStruct error when attribute is called 'method'
       [not found] <redmine.issue-15409.20181213032526@ruby-lang.org>
@ 2018-12-13  3:25 ` elioncho
  2018-12-13  5:46 ` [ruby-core:90491] " oleynikov
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: elioncho @ 2018-12-13  3:25 UTC (permalink / raw)
  To: ruby-core

Issue #15409 has been reported by elioncho (Elías Orozco).

----------------------------------------
Bug #15409: OpenStruct error when attribute is called 'method'
https://bugs.ruby-lang.org/issues/15409

* Author: elioncho (Elías Orozco)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
The following error is shown when you try to access an OpenStruct with a property called method:

`method': wrong number of arguments (given 0, expected 1) (ArgumentError)

To replicate:

~~~ ruby
require 'ostruct'
o = OpenStruct.new({ method: 'get' })
o.method
~~~


The expected behavior should be to return 'get'




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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:90491] [Ruby trunk Bug#15409] OpenStruct error when attribute is called 'method'
       [not found] <redmine.issue-15409.20181213032526@ruby-lang.org>
  2018-12-13  3:25 ` [ruby-core:90481] [Ruby trunk Bug#15409] OpenStruct error when attribute is called 'method' elioncho
@ 2018-12-13  5:46 ` oleynikov
  2018-12-13  6:17 ` [ruby-core:90492] [Ruby trunk Bug#15409][Assigned] " mame
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: oleynikov @ 2018-12-13  5:46 UTC (permalink / raw)
  To: ruby-core

Issue #15409 has been updated by oleynikov (Alexander Oleynikov).


According to the docs, `OpenStruct` uses `method_missing`, so it does not redefine existing methods of `Object` or `BasicObject`.
https://docs.ruby-lang.org/en/trunk/OpenStruct.html

```ruby
o = OpenStruct.new(
  method:  'method',
  class:   'class',
  display: 'display',
  send:    'send',
  __id__:  '__id__',
  frozen?: 'frozen?'
)

o.method  #=> ArgumentError (wrong number of arguments (given 0, expected 1))
o.class   #=> OpenStruct
o.display #=> #<OpenStruct method="method", class="class", display="display"...
o.send    #=> ArgumentError (no method name given)
o.__id__  #=> 7539827944720
o.frozen? #=> false
```

----------------------------------------
Bug #15409: OpenStruct error when attribute is called 'method'
https://bugs.ruby-lang.org/issues/15409#change-75649

* Author: elioncho (Elías Orozco)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
The following error is shown when you try to access an OpenStruct with a property called method:

`method': wrong number of arguments (given 0, expected 1) (ArgumentError)

To replicate:

~~~ ruby
require 'ostruct'
o = OpenStruct.new({ method: 'get' })
o.method
~~~


The expected behavior should be to return 'get'




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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:90492] [Ruby trunk Bug#15409][Assigned] OpenStruct error when attribute is called 'method'
       [not found] <redmine.issue-15409.20181213032526@ruby-lang.org>
  2018-12-13  3:25 ` [ruby-core:90481] [Ruby trunk Bug#15409] OpenStruct error when attribute is called 'method' elioncho
  2018-12-13  5:46 ` [ruby-core:90491] " oleynikov
@ 2018-12-13  6:17 ` mame
  2018-12-13  6:59 ` [ruby-core:90494] [Ruby trunk Bug#15409] " ruby-core
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: mame @ 2018-12-13  6:17 UTC (permalink / raw)
  To: ruby-core

Issue #15409 has been updated by mame (Yusuke Endoh).

Status changed from Open to Assigned
Assignee set to marcandre (Marc-Andre Lafortune)

Yes, the current behavior is intentional.  OpenStruct prohibits redefinition of the superclass methods.

However, the current spec that prohibits overwrite is fragile against newly introduced methods to Object class.  For example, Object#then is planned to be introduced in Ruby 2.6.  It breaks `OpenStruct({ :then => 42 })` which worked well in Ruby 2.5.

I considered this issue with some committers, and found two possible solutions:

1. Just warn if a specified key name conflicts with any method of Object class.  This does not solve the issue itself, but a user can notice the breakage.
2. Allow overwrite.  This solves the issue.  But if a user gives untrusted input as a key of OpenStruct, an attacker might be able to overwrite some basic methods (for example, Object#dup, Object#object_id, etc.), which might lead to a vulnerability of the application.  (It would be very rare, I guess, though.)

@marcandre, a maintainer of OpenStruct, what do you think?

----------------------------------------
Bug #15409: OpenStruct error when attribute is called 'method'
https://bugs.ruby-lang.org/issues/15409#change-75650

* Author: elioncho (Elías Orozco)
* Status: Assigned
* Priority: Normal
* Assignee: marcandre (Marc-Andre Lafortune)
* Target version: 
* ruby -v: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
The following error is shown when you try to access an OpenStruct with a property called method:

`method': wrong number of arguments (given 0, expected 1) (ArgumentError)

To replicate:

~~~ ruby
require 'ostruct'
o = OpenStruct.new({ method: 'get' })
o.method
~~~


The expected behavior should be to return 'get'




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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:90494] [Ruby trunk Bug#15409] OpenStruct error when attribute is called 'method'
       [not found] <redmine.issue-15409.20181213032526@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2018-12-13  6:17 ` [ruby-core:90492] [Ruby trunk Bug#15409][Assigned] " mame
@ 2018-12-13  6:59 ` ruby-core
  2018-12-13  7:23 ` [ruby-core:90497] " shevegen
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: ruby-core @ 2018-12-13  6:59 UTC (permalink / raw)
  To: ruby-core

Issue #15409 has been updated by marcandre (Marc-Andre Lafortune).


I don't have a good solution.

`OpenStruct` is kind of an anti-pattern. This is even more apparent when adding methods to `Object/Kernel`.

I note that `Struct` allows overriding builtin methods:

    Struct.new(:method, keyword_init: true).new(method: :foo).method # => :foo

I'd be tempted to allow overriding of methods in `OpenStruct` for setters (e.g. `method=`), but not very confident about it.

Even if we allow overriding like this, this could still yield to potentially unexpected results.

    o = OpenStruct.new
    o.then # => nil in Ruby 2.5, Enumerator in Ruby 2.6
    o.then = :foo  # (assuming we allow overriding from setters)
    o.then # => :foo

If the object is inited with the data like `OpenStruct.new(then: :foo)`, then both versions would at least behave the same.

----------------------------------------
Bug #15409: OpenStruct error when attribute is called 'method'
https://bugs.ruby-lang.org/issues/15409#change-75652

* Author: elioncho (Elías Orozco)
* Status: Assigned
* Priority: Normal
* Assignee: marcandre (Marc-Andre Lafortune)
* Target version: 
* ruby -v: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
The following error is shown when you try to access an OpenStruct with a property called method:

`method': wrong number of arguments (given 0, expected 1) (ArgumentError)

To replicate:

~~~ ruby
require 'ostruct'
o = OpenStruct.new({ method: 'get' })
o.method
~~~


The expected behavior should be to return 'get'




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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:90497] [Ruby trunk Bug#15409] OpenStruct error when attribute is called 'method'
       [not found] <redmine.issue-15409.20181213032526@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2018-12-13  6:59 ` [ruby-core:90494] [Ruby trunk Bug#15409] " ruby-core
@ 2018-12-13  7:23 ` shevegen
  2018-12-13  8:21 ` [ruby-core:90499] " mame
  2019-05-09 15:15 ` [ruby-core:92617] " tansaku
  6 siblings, 0 replies; 7+ messages in thread
From: shevegen @ 2018-12-13  7:23 UTC (permalink / raw)
  To: ruby-core

Issue #15409 has been updated by shevegen (Robert A. Heiler).


I think it should be overridable and a warning could be issued for those methods that would lead to breakage. I don't really use Struct and OpenStruct much at all these days though - I tend to just define what I need via simple classes.

----------------------------------------
Bug #15409: OpenStruct error when attribute is called 'method'
https://bugs.ruby-lang.org/issues/15409#change-75654

* Author: elioncho (Elías Orozco)
* Status: Assigned
* Priority: Normal
* Assignee: marcandre (Marc-Andre Lafortune)
* Target version: 
* ruby -v: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
The following error is shown when you try to access an OpenStruct with a property called method:

`method': wrong number of arguments (given 0, expected 1) (ArgumentError)

To replicate:

~~~ ruby
require 'ostruct'
o = OpenStruct.new({ method: 'get' })
o.method
~~~


The expected behavior should be to return 'get'




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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:90499] [Ruby trunk Bug#15409] OpenStruct error when attribute is called 'method'
       [not found] <redmine.issue-15409.20181213032526@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2018-12-13  7:23 ` [ruby-core:90497] " shevegen
@ 2018-12-13  8:21 ` mame
  2019-05-09 15:15 ` [ruby-core:92617] " tansaku
  6 siblings, 0 replies; 7+ messages in thread
From: mame @ 2018-12-13  8:21 UTC (permalink / raw)
  To: ruby-core

Issue #15409 has been updated by mame (Yusuke Endoh).


> `OpenStruct` is kind of an anti-pattern.

Completely agreed.  I'd like to prohibit the library itself, honestly.

> I note that Struct allows overriding builtin methods:

Interesting.  I believe no one passes untrusted keys to Struct.  But I heard that OpenStruct is used for JSON.

>     o = OpenStruct.new
>     o.then # => nil in Ruby 2.5, Enumerator in Ruby 2.6

Ah...  It's just a design flaw.

----------------------------------------
Bug #15409: OpenStruct error when attribute is called 'method'
https://bugs.ruby-lang.org/issues/15409#change-75656

* Author: elioncho (Elías Orozco)
* Status: Assigned
* Priority: Normal
* Assignee: marcandre (Marc-Andre Lafortune)
* Target version: 
* ruby -v: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
The following error is shown when you try to access an OpenStruct with a property called method:

`method': wrong number of arguments (given 0, expected 1) (ArgumentError)

To replicate:

~~~ ruby
require 'ostruct'
o = OpenStruct.new({ method: 'get' })
o.method
~~~


The expected behavior should be to return 'get'




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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:92617] [Ruby trunk Bug#15409] OpenStruct error when attribute is called 'method'
       [not found] <redmine.issue-15409.20181213032526@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2018-12-13  8:21 ` [ruby-core:90499] " mame
@ 2019-05-09 15:15 ` tansaku
  6 siblings, 0 replies; 7+ messages in thread
From: tansaku @ 2019-05-09 15:15 UTC (permalink / raw)
  To: ruby-core

Issue #15409 has been updated by tansaku (Sam Joseph).


I just encountered this issue trying to pop a json structure into an OpenStruct.  It would be great if there was a way to indicate to OpenStruct such that some keywords (e.g. `method`) are safe to be overridden in a particular context.

----------------------------------------
Bug #15409: OpenStruct error when attribute is called 'method'
https://bugs.ruby-lang.org/issues/15409#change-77976

* Author: elioncho (Elías Orozco)
* Status: Assigned
* Priority: Normal
* Assignee: marcandre (Marc-Andre Lafortune)
* Target version: 
* ruby -v: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
The following error is shown when you try to access an OpenStruct with a property called method:

`method': wrong number of arguments (given 0, expected 1) (ArgumentError)

To replicate:

~~~ ruby
require 'ostruct'
o = OpenStruct.new({ method: 'get' })
o.method
~~~


The expected behavior should be to return 'get'




-- 
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>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2019-05-09 15:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-15409.20181213032526@ruby-lang.org>
2018-12-13  3:25 ` [ruby-core:90481] [Ruby trunk Bug#15409] OpenStruct error when attribute is called 'method' elioncho
2018-12-13  5:46 ` [ruby-core:90491] " oleynikov
2018-12-13  6:17 ` [ruby-core:90492] [Ruby trunk Bug#15409][Assigned] " mame
2018-12-13  6:59 ` [ruby-core:90494] [Ruby trunk Bug#15409] " ruby-core
2018-12-13  7:23 ` [ruby-core:90497] " shevegen
2018-12-13  8:21 ` [ruby-core:90499] " mame
2019-05-09 15:15 ` [ruby-core:92617] " tansaku

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).