Skip to content

Can't set universal variables with some special characters. #1225

Closed

Description

@zanchey

Consider:

> set -U snowman ☃
> echo $snowman
☃
> set -U blah whät
> echo $blah

> set blah whät
> echo $blah
whät

Activity

  1. added this to the milestone on Mar 13, 2014
  2. siteshwar commented on Mar 16, 2014

    @siteshwar
    Contributor

    Unicode representation of whät is wh\xe4t (\xe4 = 228) which is not being decoded correctly in read_unquoted_escape method in common.cpp.

    Here is the relevant code snippet :

                    case L'x':
                    {
                        chars = 2;
                        max_val = ASCII_MAX;
                        break;
                    }
    

    Value of max_val (ASCII_MAX = 127) is too strict for this case and it will cause following test to fail :

                if (res <= max_val)
                {
                    result_char = (wchar_t)((byte_literal ? ENCODE_DIRECT_BASE : 0)+res);
                }
                else
                {
                    errored = true;
                }
    
    

    I changed case condition to :

                    case L'x':
                    {
                        chars = 2;
                        max_val = BYTE_MAX;
                        break;
                    }
    

    and set -U blah whät; echo $blah works perfectly.

    If I have not missed anything here feel free to merge #1344

  3. ridiculousfish commented on Mar 16, 2014

    @ridiculousfish
    Member

    I'm not sure about this. The fish docs are clear that \x escapes specifically ASCII strings, and \X escapes byte data.

    It looks to me like the bug is that we are generating the string 'wh\xe4t' to represent that Unicode value. It should use \X, not \x.

  4. siteshwar commented on Mar 16, 2014

    @siteshwar
    Contributor

    In python unicode("whät", "utf-8") gives 'wh\xe4t'.

  5. ridiculousfish commented on Mar 16, 2014

    @ridiculousfish
    Member

    I don't know what the original rationale was for the choice to make \x be ASCII and \X be Unicode. I think my personal preference is to make \x and \X identical, and presumably Unicode, so I think the fix is in the right direciton. However we should make that a conscious decision, and update the docs accordingly.

    We should also understand where the errant \x is coming from. Is that from fishd?

  6. siteshwar commented on Mar 16, 2014

    @siteshwar
    Contributor

    Yes, the problem was setting the variable in fishd. I have updated doc for \x through siteshwar@c8b6f58

  7. siteshwar commented on Mar 19, 2014

    @siteshwar
    Contributor

    @ridiculousfish Should I merge #1344 ?

  8. zanchey commented on Mar 20, 2014

    @zanchey
    MemberAuthor

    Perhaps we should add some tests as well.

  9. siteshwar commented on Mar 21, 2014

    @siteshwar
    Contributor

    @zanchey Updated my pull request.

  10. ridiculousfish commented on Mar 22, 2014

    @ridiculousfish
    Member

    Where's fishd generating the \x? Should it be \X instead?

  11. siteshwar commented on Mar 22, 2014

    @siteshwar
    Contributor

    I believe it's being generated in escape_string_internal method in common.cpp by fish process (and not fishd). fish sends it to fishd and then fishd fails to unescape it. \x seems to be correct since python too uses \x character to escape ä

  12. ridiculousfish commented on Mar 22, 2014

    @ridiculousfish
    Member

    Looks like it's actually being generated in full_escape in env_universal_common.cpp:

    if (c < 256)
    {
        append_format(out, L"\\x%.2x", c);
    }
    

    This used to be 128, but I changed it in 51de269 to be 256.

    I finally understand why it needs to be 128, and how these escapes are intended to work. \x and \X are both intended to produce a single byte of output. You use them to indicate that, when the given string gets converted to the ultimate narrow encoding, you want this particular byte in the output.

    The difference is that \x escapes a character, and \X escapes a byte. \x is limited to ASCII because ASCII chars are the only ones that we are sure can be represented with a single byte in the narrow encoding, which we assume is a superset of ASCII. For example, if the narrow encoding is UTF-8, only ASCII characters use a single byte.

    So how is ä represented? Is it the byte 0xE4? No! While that is its Unicode code point, it is not its representation in any encoding. Its encoding in UTF-16 is 00E4 and in UTF-8 is C3A4. It does not have a one-byte representation in any Unicode encoding, and therefore it cannot be represented using a single \x or \X escape, which only produce a single byte of output. (That's why there's the \U escape).

    Because it has a two-byte representation, and I know my terminal encoding is UTF-8, I can represent it with two byte-escapes:

    > echo \XC3\XA4
    ä
    

    This obviously is encoding-dependent, which is why the docs have the dire warning about "Only use this if you know what you are doing."

    What I cannot do is represent it with one:

    > echo \XE4
    ?
    

    I just generated an invalid UTF-8 stream: the "invalid string" that the docs warned me about.

    So I am now certain that the full_escape function's use of < 256 is quite broken. It needs to be < 128, because those are the characters whose code points match their byte values in the narrow encoding. Using <256 produces gibberish by reinterpreting code points as UTF-8 sequences.

    I'm still unsure as to whether we want to extend \x to support arbitrary byte values. We have to decide whether we want those byte values to be specified as Unicode code points as \u does, or literal bytes as \X does. I'm going to look at how other shells and languages do that.

  13. ridiculousfish commented on Mar 22, 2014

    @ridiculousfish
    Member

    Also, I should express my deep respect to axel for getting this right in the first place, and my shame at screwing it up!

  14. ridiculousfish commented on Mar 22, 2014

    @ridiculousfish
    Member

    The Python code that siteshwar shows is funny:

    >>> unicode("whät", "utf-8")
    u'wh\xe4t'
    

    Here the u prefix means that the \xe4 is a code point (or UTF-16, same thing in this case).

    The Python 3 docs say "In a bytes literal, hexadecimal and octal escapes denote the byte with the given value. In a string literal, these escapes denote a Unicode character with the given value." So in Python, \x specifies a Unicode code point.

    In zsh, \x escapes a literal byte value, and \u can be used for Unicode. \X is ignored.

    $ echo "\xE4"
    ?
    $ echo "\uE4"
    ä
    $ echo "\XE4"
    \XE4
    

    bash doesn't seem to have any way to enter hex characters, except for using echo -e.

  15. ridiculousfish commented on Mar 22, 2014

    @ridiculousfish
    Member

    I am going to revert the wrong change to the full_escape function, which should fix this bug. I spun off #1352 to discuss whether/how to extend \x to non-ASCII byte values.

  16. siteshwar commented on Mar 23, 2014

    @siteshwar
    Contributor

    Thanks for explaining, I have closed my pull request.

  17. added a commit that references this issue on Mar 26, 2014
    2e43fdc
  18. locked as resolved and limited conversation to collaborators on Apr 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions