You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "thiru-mg (via GitHub)" <gi...@apache.org> on 2024/04/02 08:07:15 UTC

[PR] Fix for wrong encoding of Unicode values above 0xffff [avro]

thiru-mg opened a new pull request, #2831:
URL: https://github.com/apache/avro/pull/2831

   <!--
   
   *Thank you very much for contributing to Apache Avro - we are happy that you want to help us improve Avro. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Avro a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/AVRO/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "AVRO-XXXX: [component] Title of the pull request", where *AVRO-XXXX* should be replaced by the actual issue number. 
       The *component* is optional, but can help identify the correct reviewers faster: either the language ("java", "python") or subsystem such as "build" or "doc" are good candidates.  
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests. You can [build the entire project](https://github.com/apache/avro/blob/main/BUILD.md) or just the [language-specific SDK](https://avro.apache.org/project/how-to-contribute/#unit-tests).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Every commit message references Jira issues in their subject lines. In addition, commits follow the guidelines from [How to write a good git commit message](https://chris.beams.io/posts/git-commit/)
       1. Subject is separated from body by a blank line
       1. Subject is limited to 50 characters (not including Jira issue reference)
       1. Subject does not end with a period
       1. Subject uses the imperative mood ("add", not "adding")
       1. Body wraps at 72 characters
       1. Body explains "what" and "why", not "how"
   
   -->
   
   ## What is the purpose of the change
   
   *(For example: This pull request improves file read performance by buffering data, fixing AVRO-XXXX.)*
   
   
   ## Verifying this change
   
   *(Please pick one of the following options)*
   
   This change is a trivial rework / code cleanup without any test coverage.
   
   *(or)*
   
   This change is already covered by existing tests, such as *(please describe tests)*.
   
   *(or)*
   
   This change added tests and can be verified as follows:
   
   *(example:)*
   - *Extended interop tests to verify consistent valid schema names between SDKs*
   - *Added test that validates that Java throws an AvroRuntimeException on invalid binary data*
   - *Manually verified the change by building the website and checking the new redirect*
   
   
   ## Documentation
   
   - Does this pull request introduce a new feature? (yes / no)
   - If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix for wrong encoding of Unicode values above 0xffff [avro]

Posted by "KalleOlaviNiemitalo (via GitHub)" <gi...@apache.org>.
KalleOlaviNiemitalo commented on code in PR #2831:
URL: https://github.com/apache/avro/pull/2831#discussion_r1547514412


##########
lang/c++/impl/json/JsonIO.cc:
##########
@@ -344,29 +370,49 @@ string JsonParser::decodeString(const string &s, bool binary) {
                 case 'u':
                 case 'U': {
                     uint32_t n = 0;
-                    char e[4];
-                    for (char &i : e) {
-                        n *= 16;
-                        char c = *++it;
-                        i = c;
-                        if (isdigit(c)) {
-                            n += c - '0';
-                        } else if (c >= 'a' && c <= 'f') {
-                            n += c - 'a' + 10;
-                        } else if (c >= 'A' && c <= 'F') {
-                            n += c - 'A' + 10;
-                        }
-                    }
+                    it = unicodeParse(it, s.end(), n);
                     if (binary) {
                         if (n > 0xff) {
                             throw Exception(boost::format(
                                                 "Invalid byte for binary: %1%%2%")
-                                            % ch % string(e, 4));
+                                            % ch % string(startSeq, ++it));
                         } else {
                             result.push_back(n);
                             continue;
                         }
                     }
+                    if (n >= 0xd800) {
+                        ++it;
+                        if (n > 0xdbff || it == s.end()) {
+                            throw Exception(boost::format(
+                                                "Invalid unicode sequence: %1%")
+                                            % string(startSeq, it));

Review Comment:
   Yes this correctly rejects unpaired surrogates between 0xD800 and 0xDFFF.  The problem is that `n >= 0xd800` also matches e.g. 0xE000 which is not a surrogate code unit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix for wrong encoding of Unicode values above 0xffff [avro]

Posted by "KalleOlaviNiemitalo (via GitHub)" <gi...@apache.org>.
KalleOlaviNiemitalo commented on code in PR #2831:
URL: https://github.com/apache/avro/pull/2831#discussion_r1547461860


##########
lang/c++/impl/json/JsonIO.cc:
##########
@@ -344,29 +370,49 @@ string JsonParser::decodeString(const string &s, bool binary) {
                 case 'u':
                 case 'U': {

Review Comment:
   Should this recognize upper-case `\U` at all?  <https://www.rfc-editor.org/rfc/rfc8259#section-7> looks like it must be lower-case `\u`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix for wrong encoding of Unicode values above 0xffff [avro]

Posted by "KalleOlaviNiemitalo (via GitHub)" <gi...@apache.org>.
KalleOlaviNiemitalo commented on code in PR #2831:
URL: https://github.com/apache/avro/pull/2831#discussion_r1547595949


##########
lang/c++/impl/json/JsonIO.cc:
##########
@@ -344,29 +370,49 @@ string JsonParser::decodeString(const string &s, bool binary) {
                 case 'u':
                 case 'U': {

Review Comment:
   My concern was that those people might attempt to use `\U` with eight hexadecimal digits (`\U0010FFFF`) like in universal character names in C.  But I suppose they wouldn't expect that to work in JSON, because JavaScript supports `\u{10FFFF}` instead of that syntax.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix for wrong encoding of Unicode values above 0xffff [avro]

Posted by "KalleOlaviNiemitalo (via GitHub)" <gi...@apache.org>.
KalleOlaviNiemitalo commented on code in PR #2831:
URL: https://github.com/apache/avro/pull/2831#discussion_r1547521157


##########
lang/c++/impl/json/JsonIO.cc:
##########
@@ -344,29 +370,49 @@ string JsonParser::decodeString(const string &s, bool binary) {
                 case 'u':
                 case 'U': {
                     uint32_t n = 0;
-                    char e[4];
-                    for (char &i : e) {
-                        n *= 16;
-                        char c = *++it;
-                        i = c;
-                        if (isdigit(c)) {
-                            n += c - '0';
-                        } else if (c >= 'a' && c <= 'f') {
-                            n += c - 'a' + 10;
-                        } else if (c >= 'A' && c <= 'F') {
-                            n += c - 'A' + 10;
-                        }
-                    }
+                    it = unicodeParse(it, s.end(), n);
                     if (binary) {
                         if (n > 0xff) {
                             throw Exception(boost::format(
                                                 "Invalid byte for binary: %1%%2%")
-                                            % ch % string(e, 4));
+                                            % ch % string(startSeq, ++it));
                         } else {
                             result.push_back(n);
                             continue;
                         }
                     }
+                    if (n >= 0xd800) {
+                        ++it;
+                        if (n > 0xdbff || it == s.end()) {
+                            throw Exception(boost::format(
+                                                "Invalid unicode sequence: %1%")
+                                            % string(startSeq, it));

Review Comment:
   So the check should be `if (n >= 0xd800 && n < 0xe000)` or something equivalent.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix for wrong encoding of Unicode values above 0xffff [avro]

Posted by "thiru-mg (via GitHub)" <gi...@apache.org>.
thiru-mg commented on code in PR #2831:
URL: https://github.com/apache/avro/pull/2831#discussion_r1547671476


##########
lang/c++/impl/json/JsonIO.cc:
##########
@@ -344,29 +370,49 @@ string JsonParser::decodeString(const string &s, bool binary) {
                 case 'u':
                 case 'U': {
                     uint32_t n = 0;
-                    char e[4];
-                    for (char &i : e) {
-                        n *= 16;
-                        char c = *++it;
-                        i = c;
-                        if (isdigit(c)) {
-                            n += c - '0';
-                        } else if (c >= 'a' && c <= 'f') {
-                            n += c - 'a' + 10;
-                        } else if (c >= 'A' && c <= 'F') {
-                            n += c - 'A' + 10;
-                        }
-                    }
+                    it = unicodeParse(it, s.end(), n);
                     if (binary) {
                         if (n > 0xff) {
                             throw Exception(boost::format(
                                                 "Invalid byte for binary: %1%%2%")
-                                            % ch % string(e, 4));
+                                            % ch % string(startSeq, ++it));
                         } else {
                             result.push_back(n);
                             continue;
                         }
                     }
+                    if (n >= 0xd800) {
+                        ++it;
+                        if (n > 0xdbff || it == s.end()) {
+                            throw Exception(boost::format(
+                                                "Invalid unicode sequence: %1%")
+                                            % string(startSeq, it));

Review Comment:
   Of the two escape sequences for code-points `0x10000` to `0x110000`, the first one must be between `0xd800` and `0xdbff` and the second one must be between `0xdc00` and `0xdfff`. And these rages cannot be used standalone or in the reverse order. Fixed it so that the first sequence has value between `0xd800`  (inclusive) and `0xdc00` (exclusive)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix for wrong encoding of Unicode values above 0xffff [avro]

Posted by "thiru-mg (via GitHub)" <gi...@apache.org>.
thiru-mg commented on code in PR #2831:
URL: https://github.com/apache/avro/pull/2831#discussion_r1547671476


##########
lang/c++/impl/json/JsonIO.cc:
##########
@@ -344,29 +370,49 @@ string JsonParser::decodeString(const string &s, bool binary) {
                 case 'u':
                 case 'U': {
                     uint32_t n = 0;
-                    char e[4];
-                    for (char &i : e) {
-                        n *= 16;
-                        char c = *++it;
-                        i = c;
-                        if (isdigit(c)) {
-                            n += c - '0';
-                        } else if (c >= 'a' && c <= 'f') {
-                            n += c - 'a' + 10;
-                        } else if (c >= 'A' && c <= 'F') {
-                            n += c - 'A' + 10;
-                        }
-                    }
+                    it = unicodeParse(it, s.end(), n);
                     if (binary) {
                         if (n > 0xff) {
                             throw Exception(boost::format(
                                                 "Invalid byte for binary: %1%%2%")
-                                            % ch % string(e, 4));
+                                            % ch % string(startSeq, ++it));
                         } else {
                             result.push_back(n);
                             continue;
                         }
                     }
+                    if (n >= 0xd800) {
+                        ++it;
+                        if (n > 0xdbff || it == s.end()) {
+                            throw Exception(boost::format(
+                                                "Invalid unicode sequence: %1%")
+                                            % string(startSeq, it));

Review Comment:
   Of the two escape sequences for code-points `0x10000` to `0x110000`, the first one must be between `0xd800` and `0xdbff` and the second one must be between `0xdc00` and `0xdfff`. And these rages cannot be used standalone or in the reverse order.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3860: Fix for wrong encoding of Unicode values above 0xffff [avro]

Posted by "KalleOlaviNiemitalo (via GitHub)" <gi...@apache.org>.
KalleOlaviNiemitalo commented on code in PR #2831:
URL: https://github.com/apache/avro/pull/2831#discussion_r1554509918


##########
lang/c++/impl/json/JsonIO.cc:
##########
@@ -350,30 +372,42 @@ string JsonParser::decodeString(const string &s, bool binary) {
                     continue;
                 case 'u':
                 case 'U': {
-                    uint32_t n = 0;
-                    char e[4];
-                    for (char &i : e) {
-                        n *= 16;
-                        char c = readNextByte(it, end);
-                        i = c;
-                        if (isdigit(c)) {
-                            n += c - '0';
-                        } else if (c >= 'a' && c <= 'f') {
-                            n += c - 'a' + 10;
-                        } else if (c >= 'A' && c <= 'F') {
-                            n += c - 'A' + 10;
-                        }
-                    }
+                    uint32_t n = unicodeParse();
                     if (binary) {
                         if (n > 0xff) {
                             throw Exception(boost::format(
                                                 "Invalid byte for binary: %1%%2%")
-                                            % ch % string(e, 4));
+                                            % ch % string(startSeq, ++it));
                         } else {
                             result.push_back(n);
                             continue;
                         }
                     }
+                    if (n >= 0xd800 && n < 0xdc00) {
+                        ch = readNextByte();
+                        if (ch != '\\') {
+                            throw Exception(boost::format(
+                                                "Invalid unicode sequence: %1%")
+                                            % string(startSeq, it));
+                        }
+                        ch = readNextByte();
+                        if (ch != 'u' && ch != 'U') {
+                            throw Exception(boost::format(
+                                                "Invalid unicode sequence: %1%")
+                                            % string(startSeq, it));
+                        }
+                        uint32_t m = unicodeParse();
+                        if (m < 0xdc00 || m > 0xdfff) {
+                            throw Exception(boost::format(
+                                                "Invalid unicode sequence: %1%")
+                                            % string(startSeq, it));
+                        }
+                        n = 0x10000 + (((n - 0xd800) << 10) | (m - 0xdc00));
+                    } else if (n >= 0xdc00 && n < 0xdfff) {

Review Comment:
   This still doesn’t look right; it treats 0xdfff differently from 0xdffe even though both of these are surrogate code units. I think the second comparison should instead be `n <= 0xdfff` or `n < 0xe000`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix for wrong encoding of Unicode values above 0xffff [avro]

Posted by "thiru-mg (via GitHub)" <gi...@apache.org>.
thiru-mg commented on code in PR #2831:
URL: https://github.com/apache/avro/pull/2831#discussion_r1547559727


##########
lang/c++/impl/json/JsonIO.cc:
##########
@@ -344,29 +370,49 @@ string JsonParser::decodeString(const string &s, bool binary) {
                 case 'u':
                 case 'U': {

Review Comment:
   The spec needs only lower-case `u`. But sometimes people use upper case as well, so we take a defensive position. The behavior is not new,



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix for wrong encoding of Unicode values above 0xffff [avro]

Posted by "KalleOlaviNiemitalo (via GitHub)" <gi...@apache.org>.
KalleOlaviNiemitalo commented on code in PR #2831:
URL: https://github.com/apache/avro/pull/2831#discussion_r1547685173


##########
lang/c++/impl/json/JsonIO.cc:
##########
@@ -344,29 +370,49 @@ string JsonParser::decodeString(const string &s, bool binary) {
                 case 'u':
                 case 'U': {
                     uint32_t n = 0;
-                    char e[4];
-                    for (char &i : e) {
-                        n *= 16;
-                        char c = *++it;
-                        i = c;
-                        if (isdigit(c)) {
-                            n += c - '0';
-                        } else if (c >= 'a' && c <= 'f') {
-                            n += c - 'a' + 10;
-                        } else if (c >= 'A' && c <= 'F') {
-                            n += c - 'A' + 10;
-                        }
-                    }
+                    it = unicodeParse(it, s.end(), n);
                     if (binary) {
                         if (n > 0xff) {
                             throw Exception(boost::format(
                                                 "Invalid byte for binary: %1%%2%")
-                                            % ch % string(e, 4));
+                                            % ch % string(startSeq, ++it));
                         } else {
                             result.push_back(n);
                             continue;
                         }
                     }
+                    if (n >= 0xd800) {
+                        ++it;
+                        if (n > 0xdbff || it == s.end()) {
+                            throw Exception(boost::format(
+                                                "Invalid unicode sequence: %1%")
+                                            % string(startSeq, it));

Review Comment:
   The new implementation looks like it will accept an unpaired surrogate `\udc00` without complaint and encode it to invalid UTF-8.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix for wrong encoding of Unicode values above 0xffff [avro]

Posted by "KalleOlaviNiemitalo (via GitHub)" <gi...@apache.org>.
KalleOlaviNiemitalo commented on code in PR #2831:
URL: https://github.com/apache/avro/pull/2831#discussion_r1547475060


##########
lang/c++/impl/json/JsonIO.cc:
##########
@@ -344,29 +370,49 @@ string JsonParser::decodeString(const string &s, bool binary) {
                 case 'u':
                 case 'U': {
                     uint32_t n = 0;
-                    char e[4];
-                    for (char &i : e) {
-                        n *= 16;
-                        char c = *++it;
-                        i = c;
-                        if (isdigit(c)) {
-                            n += c - '0';
-                        } else if (c >= 'a' && c <= 'f') {
-                            n += c - 'a' + 10;
-                        } else if (c >= 'A' && c <= 'F') {
-                            n += c - 'A' + 10;
-                        }
-                    }
+                    it = unicodeParse(it, s.end(), n);
                     if (binary) {
                         if (n > 0xff) {
                             throw Exception(boost::format(
                                                 "Invalid byte for binary: %1%%2%")
-                                            % ch % string(e, 4));
+                                            % ch % string(startSeq, ++it));
                         } else {
                             result.push_back(n);
                             continue;
                         }
                     }
+                    if (n >= 0xd800) {
+                        ++it;
+                        if (n > 0xdbff || it == s.end()) {
+                            throw Exception(boost::format(
+                                                "Invalid unicode sequence: %1%")
+                                            % string(startSeq, it));

Review Comment:
   This incorrectly rejects code points in the U+E000 to U+FFFF range.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix for wrong encoding of Unicode values above 0xffff [avro]

Posted by "KalleOlaviNiemitalo (via GitHub)" <gi...@apache.org>.
KalleOlaviNiemitalo commented on code in PR #2831:
URL: https://github.com/apache/avro/pull/2831#discussion_r1547468328


##########
lang/c++/impl/json/JsonIO.cc:
##########
@@ -314,11 +314,37 @@ JsonParser::Token JsonParser::tryString() {
     }
 }
 
+static string::const_iterator unicodeParse(string::const_iterator b, string::const_iterator e, uint32_t &n) {
+    string::const_iterator start = b;
+    for (int i = 0; i < 4; i++) {
+        ++b;
+        if (b == e) {
+            throw Exception(boost::format(
+                                "Invalid unicode escape: %1%") % string(start, b));

Review Comment:
   `string(start, b)` seems to be formatted like `u123`. Should the exception message include the backslash as in `\u123`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix for wrong encoding of Unicode values above 0xffff [avro]

Posted by "thiru-mg (via GitHub)" <gi...@apache.org>.
thiru-mg commented on code in PR #2831:
URL: https://github.com/apache/avro/pull/2831#discussion_r1547508566


##########
lang/c++/impl/json/JsonIO.cc:
##########
@@ -344,29 +370,49 @@ string JsonParser::decodeString(const string &s, bool binary) {
                 case 'u':
                 case 'U': {
                     uint32_t n = 0;
-                    char e[4];
-                    for (char &i : e) {
-                        n *= 16;
-                        char c = *++it;
-                        i = c;
-                        if (isdigit(c)) {
-                            n += c - '0';
-                        } else if (c >= 'a' && c <= 'f') {
-                            n += c - 'a' + 10;
-                        } else if (c >= 'A' && c <= 'F') {
-                            n += c - 'A' + 10;
-                        }
-                    }
+                    it = unicodeParse(it, s.end(), n);
                     if (binary) {
                         if (n > 0xff) {
                             throw Exception(boost::format(
                                                 "Invalid byte for binary: %1%%2%")
-                                            % ch % string(e, 4));
+                                            % ch % string(startSeq, ++it));
                         } else {
                             result.push_back(n);
                             continue;
                         }
                     }
+                    if (n >= 0xd800) {
+                        ++it;
+                        if (n > 0xdbff || it == s.end()) {
+                            throw Exception(boost::format(
+                                                "Invalid unicode sequence: %1%")
+                                            % string(startSeq, it));

Review Comment:
   Unicode code points between `0x10000` and `0x110000` are encoded as two `\uxxxx\uyyyy` where `xxxx` is in the range `0xd800` to `0xdbff` and `yyyy` is in the range `0xdc00` to `0xdffff`. The range from `0xd800` to `0xdf00` are reserved only for this encoding and no true Unicode chacters are defined there. Please see section 2 of https://www.ietf.org/rfc/rfc2781.txt: `Note: Values between 0xD800 and 0xDFFF are specifically reserved for
      use with UTF-16, and don't have any characters assigned to them.`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix for wrong encoding of Unicode values above 0xffff [avro]

Posted by "thiru-mg (via GitHub)" <gi...@apache.org>.
thiru-mg commented on code in PR #2831:
URL: https://github.com/apache/avro/pull/2831#discussion_r1547854046


##########
lang/c++/impl/json/JsonIO.cc:
##########
@@ -344,29 +370,49 @@ string JsonParser::decodeString(const string &s, bool binary) {
                 case 'u':
                 case 'U': {
                     uint32_t n = 0;
-                    char e[4];
-                    for (char &i : e) {
-                        n *= 16;
-                        char c = *++it;
-                        i = c;
-                        if (isdigit(c)) {
-                            n += c - '0';
-                        } else if (c >= 'a' && c <= 'f') {
-                            n += c - 'a' + 10;
-                        } else if (c >= 'A' && c <= 'F') {
-                            n += c - 'A' + 10;
-                        }
-                    }
+                    it = unicodeParse(it, s.end(), n);
                     if (binary) {
                         if (n > 0xff) {
                             throw Exception(boost::format(
                                                 "Invalid byte for binary: %1%%2%")
-                                            % ch % string(e, 4));
+                                            % ch % string(startSeq, ++it));
                         } else {
                             result.push_back(n);
                             continue;
                         }
                     }
+                    if (n >= 0xd800) {
+                        ++it;
+                        if (n > 0xdbff || it == s.end()) {
+                            throw Exception(boost::format(
+                                                "Invalid unicode sequence: %1%")
+                                            % string(startSeq, it));

Review Comment:
   Again, nice catch. Fixed it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix for wrong encoding of Unicode values above 0xffff [avro]

Posted by "thiru-mg (via GitHub)" <gi...@apache.org>.
thiru-mg commented on code in PR #2831:
URL: https://github.com/apache/avro/pull/2831#discussion_r1547671738


##########
lang/c++/impl/json/JsonIO.cc:
##########
@@ -314,11 +314,37 @@ JsonParser::Token JsonParser::tryString() {
     }
 }
 
+static string::const_iterator unicodeParse(string::const_iterator b, string::const_iterator e, uint32_t &n) {
+    string::const_iterator start = b;
+    for (int i = 0; i < 4; i++) {
+        ++b;
+        if (b == e) {
+            throw Exception(boost::format(
+                                "Invalid unicode escape: %1%") % string(start, b));

Review Comment:
   Good catch. Fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-3860: Fix for wrong encoding of Unicode values above 0xffff [avro]

Posted by "thiru-mg (via GitHub)" <gi...@apache.org>.
thiru-mg merged PR #2831:
URL: https://github.com/apache/avro/pull/2831


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org