Skip to content

SafeBuffer#gsub is broken #8

@ConradIrwin

Description

@ConradIrwin

The latest patch breaks $-variable binding in the callback to gsub on SafeStrings:

before:
"a".html_safe.gsub(/(a)/) { $1 } == "a"
after:
"a".html_safe.gsub(/(a)/) { $1 } == ""

We noticed this because CGI::escape uses this behaviour, so if you pass a safe buffer as a URL argument to a url helper, you will get an exception, a snippet from our codebase:

link_to "Please log in", login_url(:return_to => url_for(params))

# The error occurred while evaluating nil.size
# /home/conrad/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/cgi.rb:343:in `escape'

This is because $ variables are bound in the scope of the immediate caller to String#gsub not in the scope of the block you pass in.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions