Skip to content

Commit

Permalink
Add null terminator for char* based json string unescape, and precond…
Browse files Browse the repository at this point in the history
…ition on input length (Azure#2303)

* Add null terminator for char* based json string unescape, and precondition on input length

* Add remark comment about precondition and null terimination

* Update doc comment typo

* Remove the test cases about insufficient space.

* Fix clang formatting.

* Add null terminator tests.

* Test that the null terminator is there at the end.

* Fix build errors.
  • Loading branch information
danewalton committed Sep 15, 2022
1 parent d84c438 commit 47065d5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
18 changes: 18 additions & 0 deletions sdk/src/azure/core/az_json_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ AZ_NODISCARD AZ_INLINE _az_json_stack_item _az_json_stack_peek(_az_json_bit_stac
: _az_JSON_STACK_ARRAY;
}

AZ_NODISCARD AZ_INLINE bool _az_is_valid_escaped_character(uint8_t byte)
{
switch (byte)
{
case '\\':
case '"':
case '/':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
return true;
default:
return false;
}
}

#include <azure/core/_az_cfg_suffix.h>

#endif // _az_SPAN_PRIVATE_H
18 changes: 0 additions & 18 deletions sdk/src/azure/core/az_json_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,24 +237,6 @@ AZ_NODISCARD static az_result _az_json_reader_process_container_start(
return AZ_OK;
}

AZ_NODISCARD static bool _az_is_valid_escaped_character(uint8_t byte)
{
switch (byte)
{
case '\\':
case '"':
case '/':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
return true;
default:
return false;
}
}

AZ_NODISCARD static az_result _az_json_reader_process_string(az_json_reader* ref_json_reader)
{
// Move past the first '"' character
Expand Down
9 changes: 7 additions & 2 deletions sdk/tests/core/test_az_json.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
#define strdup _strdup
#endif

#include "az_test_definitions.h"
#include <azure/core/az_json.h>
Expand All @@ -13,7 +16,7 @@
#include <cmocka.h>

#include <azure/core/_az_cfg.h>

#include <stdlib.h>
#define TEST_EXPECT_SUCCESS(exp) assert_true(az_result_succeeded(exp))

az_result test_allocator(
Expand Down Expand Up @@ -3373,6 +3376,8 @@ int test_az_json()
cmocka_unit_test(test_az_json_token_number_too_large),
cmocka_unit_test(test_az_json_token_literal),
cmocka_unit_test(test_az_json_token_copy),
cmocka_unit_test(test_az_json_reader_chunked) };
cmocka_unit_test(test_az_json_reader_chunked),
cmocka_unit_test(test_az_json_string_unescape),
cmocka_unit_test(test_az_json_string_unescape_same_buffer) };
return cmocka_run_group_tests_name("az_core_json", tests, NULL, NULL);
}

0 comments on commit 47065d5

Please sign in to comment.