Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unreachable code path in Fiddle::ValueUtil#wrap_arg #124

Open
Maumagnaguagno opened this issue Mar 17, 2023 · 5 comments
Open

Unreachable code path in Fiddle::ValueUtil#wrap_arg #124

Maumagnaguagno opened this issue Mar 17, 2023 · 5 comments

Comments

@Maumagnaguagno
Copy link
Contributor

In Fiddle::ValueUtil#wrap_arg there is a long case-when for arg with an unreachable code path.
When arg and ty are of Array type, and ty[0] is TYPE_CHAR, there is another condition, for arg to be String at Line 101.
Even if this condition is fixed, val is undefined at Line 102 for this method.

fiddle/lib/fiddle/value.rb

Lines 95 to 106 in 04238ce

when Array
if( ty.is_a?(Array) ) # used only by struct
case ty[0]
when TYPE_VOIDP
return arg.collect{|v| Integer(v)}
when TYPE_CHAR
if( arg.is_a?(String) )
return val.unpack('C*')
end
end
end
return arg

Should Lines 100-103 be removed?

@kou
Copy link
Member

kou commented Mar 17, 2023

Can you create a script that uses 101 and share it to us?

@Maumagnaguagno
Copy link
Contributor Author

require 'fiddle/value'

c = Class.new.extend(Fiddle::ValueUtil)
p c.wrap_arg(['55','0'],[Fiddle::TYPE_VOIDP]) # Line  99
p c.wrap_arg(['test'],[Fiddle::TYPE_CHAR])    # Line 106

@kou
Copy link
Member

kou commented Mar 17, 2023

Ah, could you use normal API instead of directly using wrap_arg? It seems that lib/fiddle/struct.rb uses wrap_arg.

@Maumagnaguagno
Copy link
Contributor Author

Unfortunately I am not that familiar with Fiddle structs to make a valid minimal example to reach that point.
I just noticed the loose variable val and that arg needs to be both Array and String at the same time.

@kou
Copy link
Member

kou commented Mar 20, 2023

OK.
I'll take a look at this later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants