Repository navigation
[fix](jsonb) Stop truncating JSON text at a U+0000 inside a string #68244
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
Merged
+125
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
regression-test/data/query_p0/sql_functions/table_function/test_json_text_embedded_nul.out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| -- This file is automatically generated. You should know what you did if you want to edit this | ||
| -- !json_each_text_nested_array -- | ||
| x 12 ["a\\u0000b"] | ||
|
|
||
| -- !json_each_text_nested_object -- | ||
| x 16 {"y":"a\\u0000b"} | ||
|
|
||
| -- !json_each_text_trailing_nul -- | ||
| x 20 ["a\\u0000","\\u0000"] | ||
|
|
||
| -- !explode_json_array_string_direct -- | ||
| 3 610062 | ||
|
|
||
| -- !explode_json_array_string_nested -- | ||
| 12 ["a\\u0000b"] | ||
|
|
||
| -- !explode_json_array_string_trailing_nul -- | ||
| 11 ["a\\u0000"] | ||
|
|
62 changes: 62 additions & 0 deletions
62
...sion-test/suites/query_p0/sql_functions/table_function/test_json_text_embedded_nul.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| suite("test_json_text_embedded_nul") { | ||
| // unhex('5C') is a backslash, so every JSON literal below really contains | ||
| // \u0000, the legal escape for U+0000 inside a JSON string. Rendering such | ||
| // a nested value back to text must not stop at the NUL. | ||
|
|
||
| qt_json_each_text_nested_array ''' | ||
| SELECT k, length(v), v | ||
| FROM (SELECT 1) d | ||
| LATERAL VIEW json_each_text(concat('{"x":["a', unhex('5C'), 'u0000b"]}')) t AS k, v | ||
| ''' | ||
|
|
||
| qt_json_each_text_nested_object ''' | ||
| SELECT k, length(v), v | ||
| FROM (SELECT 1) d | ||
| LATERAL VIEW json_each_text(concat('{"x":{"y":"a', unhex('5C'), 'u0000b"}}')) t AS k, v | ||
| ''' | ||
|
|
||
| // A NUL at the very end of a string used to be trimmed by the stored-length | ||
| // helper, so keep a dedicated case for it. | ||
| qt_json_each_text_trailing_nul ''' | ||
| SELECT k, length(v), v | ||
| FROM (SELECT 1) d | ||
| LATERAL VIEW json_each_text(concat('{"x":["a', unhex('5C'), 'u0000","', unhex('5C'), 'u0000"]}')) t AS k, v | ||
| ''' | ||
|
|
||
| // A direct string element is copied by length, so it keeps the raw NUL byte; | ||
| // compare it through hex() to keep the expected output printable. | ||
| qt_explode_json_array_string_direct ''' | ||
| SELECT length(c), hex(c) | ||
| FROM (SELECT 1) d | ||
| LATERAL VIEW explode_json_array_string(concat('["a', unhex('5C'), 'u0000b"]')) t AS c | ||
| ''' | ||
|
|
||
| qt_explode_json_array_string_nested ''' | ||
| SELECT length(c), c | ||
| FROM (SELECT 1) d | ||
| LATERAL VIEW explode_json_array_string(concat('[["a', unhex('5C'), 'u0000b"]]')) t AS c | ||
| ''' | ||
|
|
||
| qt_explode_json_array_string_trailing_nul ''' | ||
| SELECT length(c), c | ||
| FROM (SELECT 1) d | ||
| LATERAL VIEW explode_json_array_string(concat('[["a', unhex('5C'), 'u0000"]]')) t AS c | ||
| ''' | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Preserve the stored length for a trailing U+0000
This loop is length-bounded now, but
intern_json()still calls it withJsonbStringVal::length()(lines 110-111). That helper returnsstrnlen(payload, size)whenever the last stored byte is zero. As a result, a legal JSON string whose decoded payload is the two bytesa,0reaches this loop withlen == 1and still renders as justa; a value whose decoded payload is only the zero byte becomes empty. All new tests putbafter the NUL, so they miss this branch. Please use the authoritative payload length here (for example,getBlobLen()in theT_Stringcase, while handling any typed padding at its type-aware producer) and add trailing/only-NUL coverage.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, fixed in 67d0df8.
intern_json()now passesgetBlobLen()for theT_Stringcase, so the storedpayload length reaches the length-bounded loop unchanged.
JsonbWriterwrites astring with
writeString(str, len)and patches the size field with the exactnumber of bytes written, so it never emits NUL padding and
getBlobLen()is theauthoritative length here; the direct string path in
vjson_each.cppalreadyrelies on the same helper.
New coverage for the branch you pointed at:
JsonbParserTest.ParseJsonWithTrailingNulInString({"key":"a\u0000"})JsonbParserTest.ParseJsonWithOnlyNulInString({"key":"\u0000"})JsonbParserTest.ParseJsonWithTrailingNulInNestedArray({"key":["a\u0000","\u0000"]})json_each_text_trailing_nulandexplode_json_array_string_trailing_nulThe regression output regenerated on a local cluster now shows
["a\u0000","\u0000"](length 20) and["a\u0000"](length 11) where the oldcode returned
["a",""]and["a"].Note that
JsonbStringVal::length()has the same trimming behaviour for itsother callers (the string equality check in
jsonb_document.hand two spots infunction_jsonb.cpp). Those are separate semantics from JSON textualization, soI left them out of this fix rather than widening the diff.