sass/libsass — pull request #780 by smikes (2014-12-27 04:51:08+00:00)
Title: (DO NOT MERGE) 694 unquoting problems

This is a reference PR, not finished yet, and not intended for merging.

I am trying to approach the problems in #694 (interpolation of quoted strings differs from Ruby) by writing some systematic unit tests to cover the `Sass::unquote()` and `Sass:quote()` function. To write these unit tests, I am using the C++ unit test framework `catch` as a module pulled from `npm`, so I have added a `package.json` -- this file is purely temporary and will not be included in the final PR, but is included for the moment if anyone is interested in playing with catch and/or reproducing my results.

So far I have done the following:

1. factor out `Sass::quote` and `Sass::unquote` into a separate file for testing
2. add unit tests to cover the branches in `Sass::unquote`.

I see at least two problems so far. One is an underread of allocated memory when a quoted string contains an unescaped quote -- i.e., when calling `Sass::unquote("\"\"Hello\"")`. When the argument is `""Hello"` then unquote treats it as a quoted string, but then calls `t.erase(t.length()-1)` when `t.length()` is zero. This is what triggers the exception (`basic_string`) thrown in case # 694.

The second problem is slightly subtler: When unquoting a string, backslash is not treated as a special character. This causes two problems: one is that backslashes are not unescaped correctly -- `Sass::unquote("'\\\\'")` yields two backslashes, but should only yield one. The second problem is that unescaped quotes inside strings are not detected; `Sass::unquote` just assumes that the character preceding a quote in a quoted string **is** a backslash, resulting in erroneous unquoting: `Sass::unquote("'foo \'bar' baz'")` yields `foo 'ba' baz` because `unquote` assumes that the `r` before the `'` is a backslash and deletes it.