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

Fix escape_for_sql method #358

Merged
merged 1 commit into from
Jan 15, 2021
Merged

Conversation

juananpe
Copy link
Contributor

The previous version was quite broken:

def escape_for_sql(s)
s = s.to_s
if s.nil?
"''"
else
"'" + s.tr("'", "\'") + "'"
end
end

Specifically, this line is not doing what we suppose it'll do:

"'" + s.tr("'", "\'") + "'"

Apparently, it seems that a string like escape'me would be translated to escape\'me but that's not the case:

2.7.2 :055 > "escape'me".tr("'","\'")
 => "escape'me"

You can find a good explanation of why here.

So the escape_for_sql method wasn't really escaping anything o_O

Finally, I think that this if branch is unneded:

if s.nil?
"''"
else

In fact, nil.to_s surprisingly works (it would be a clear npe exception in Java!) and casts nil to an empty string "", making the whole if block superfluous.

2.7.2 :046 > b=nil
 => nil
2.7.2 :047 > b.nil?
 => true
2.7.2 :049 > b.to_s
 => ""
2.7.2 :050 > b.to_s.nil?
 => false

@urbanadventurer
Copy link
Owner

Thanks @juananpe! I really like how detailed you were with this bug! There must not have been many people using the SQL logging for this issue to have gone unnoticed for so long!

@urbanadventurer urbanadventurer merged commit 982dc2d into urbanadventurer:master Jan 15, 2021
@juananpe juananpe deleted the sql branch January 15, 2021 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants