JSONSerialization crashes when .sortedKeys is used

Originator:marcelofabri.mf
Number:rdar://38796619 Date Originated:23-Mar-2018 09:52 AM
Status:Open Resolved:
Product:macOS + SDK Product Version:10.13.3
Classification:Crash/Hang/Data Loss Reproducible:Always
 
Summary:
JSONSerialization crashes with long strings that are equal if compared case-insensitive.

This also happens in Objective-C with NSJSONSerialization

Steps to Reproduce:
Paste this into a playground/project and run:

import Cocoa

let k1 = "[\"/Users/marcelofabri/SwiftLint/source\",[[\"block_based_kvo\",\"warning\"],[\"class_delegate_protocol\",\"warning\"],[\"closing_brace\",\"warning\"],[\"closure_parameter_position\",\"warning\"],[\"colon\",\"warning, flexible_right_spacing: false, apply_to_dictionaries: true\"],[\"comma\",\"warning\"],[\"compiler_protocol_init\",\"warning\"],[\"control_statement\",\"warning\"],[\"custom_rules\",\"\"],[\"cyclomatic_complexity\",\"warning: 10, error: 20, ignores_case_statements: false\"],[\"discarded_notification_center_observer\",\"warning\"],[\"discouraged_direct_init\",\"warning, types: [\"Bundle\", \"Bundle.init\", \"UIDevice\", \"UIDevice.init\"]\"],[\"dynamic_inline\",\"error\"],[\"empty_enum_arguments\",\"warning\"],[\"empty_parameters\",\"warning\"],[\"empty_parentheses_with_trailing_closure\",\"warning\"],[\"fallthrough\",\"warning\"],[\"file_length\",\"warning: 400, error: 1000ignore_comment_only_lines: false\"],[\"for_where\",\"warning\"],[\"force_cast\",\"error\"],[\"force_try\",\"error\"],[\"function_body_length\",\"warning: 40, error: 100\"],[\"function_parameter_count\",\"warning: 5, error: 8\"],[\"generic_type_name\",\"(min_length) w/e: 1/0, (max_length) w/e: 20/1000, excluded: [], allowed_symbols: [], validates_start_with_lowercase: true\"],[\"identifier_name\",\"(min_length) w/e: 3/2, (max_length) w/e: 40/60, excluded: [], allowed_symbols: [], validates_start_with_lowercase: true\"],[\"implicit_getter\",\"warning\"],[\"is_disjoint\",\"warning\"],[\"large_tuple\",\"warning: 2, error: 3\"],[\"leading_whitespace\",\"warning\"],[\"legacy_cggeometry_functions\",\"warning\"],[\"legacy_constant\",\"warning\"],[\"legacy_constructor\",\"warning\"],[\"legacy_nsgeometry_functions\",\"warning\"],[\"line_length\",\"warning: 120, error: 200, ignores urls: false, ignores function declarations: false, ignores comments: false\"],[\"mark\",\"warning\"],[\"multiple_closures_with_trailing_closure\",\"warning\"],[\"nesting\",\"(type_level) w: 1, (statement_level) w: 5\"],[\"notification_center_detachment\",\"warning\"],[\"opening_brace\",\"warning\"],[\"operator_whitespace\",\"warning\"],[\"private_over_fileprivate\",\"warning, validate_extensions: false\"],[\"private_unit_test\",\"[\"private_unit_test\",\"\",\"Unit test marked `private` will not be run by XCTest.\",\"XCTestCase\",\"\",\"warning\"]\"],[\"protocol_property_accessors_order\",\"warning\"],[\"redundant_discardable_let\",\"warning\"],[\"redundant_optional_initialization\",\"warning\"],[\"redundant_string_enum_value\",\"warning\"],[\"redundant_void_return\",\"warning\"],[\"return_arrow_whitespace\",\"warning\"],[\"shorthand_operator\",\"error\"],[\"statement_position\",\"(statement_mode) default, (severity) warning\"],[\"superfluous_disable_command\",\"error\"],[\"switch_case_alignment\",\"warning\"],[\"syntactic_sugar\",\"warning\"],[\"todo\",\"warning\"],[\"trailing_comma\",\"warning, mandatory_comma: false\"],[\"trailing_newline\",\"warning\"],[\"trailing_semicolon\",\"warning\"],[\"trailing_whitespace\",\"warning, ignores_empty_lines: false, ignores_comments: true\"],[\"type_body_length\",\"warning: 200, error: 350\"],[\"type_name\",\"(min_length) w/e: 3/0, (max_length) w/e: 40/1000, excluded: [], allowed_symbols: [], validates_start_with_lowercase: true\"],[\"unneeded_break_in_switch\",\"warning\"],[\"unused_closure_parameter\",\"warning\"],[\"unused_enumerated\",\"warning\"],[\"unused_optional_binding\",\"warning, ignore_optional_try: false\"],[\"valid_ibinspectable\",\"warning\"],[\"vertical_parameter_alignment\",\"warning\"],[\"vertical_whitespace\",\"warning, max_empty_lines: 1\"],[\"void_return\",\"warning\"],[\"weak_delegate\",\"warning\"],[\"xctfail_message\",\"warning\"]]]"

let k2 = "[\"/Users/marcelofabri/SwiftLint/Source\",[[\"block_based_kvo\",\"warning\"],[\"class_delegate_protocol\",\"warning\"],[\"closing_brace\",\"warning\"],[\"closure_parameter_position\",\"warning\"],[\"colon\",\"warning, flexible_right_spacing: false, apply_to_dictionaries: true\"],[\"comma\",\"warning\"],[\"compiler_protocol_init\",\"warning\"],[\"control_statement\",\"warning\"],[\"custom_rules\",\"\"],[\"cyclomatic_complexity\",\"warning: 10, error: 20, ignores_case_statements: false\"],[\"discarded_notification_center_observer\",\"warning\"],[\"discouraged_direct_init\",\"warning, types: [\"Bundle\", \"Bundle.init\", \"UIDevice\", \"UIDevice.init\"]\"],[\"dynamic_inline\",\"error\"],[\"empty_enum_arguments\",\"warning\"],[\"empty_parameters\",\"warning\"],[\"empty_parentheses_with_trailing_closure\",\"warning\"],[\"fallthrough\",\"warning\"],[\"file_length\",\"warning: 400, error: 1000ignore_comment_only_lines: false\"],[\"for_where\",\"warning\"],[\"force_cast\",\"error\"],[\"force_try\",\"error\"],[\"function_body_length\",\"warning: 40, error: 100\"],[\"function_parameter_count\",\"warning: 5, error: 8\"],[\"generic_type_name\",\"(min_length) w/e: 1/0, (max_length) w/e: 20/1000, excluded: [], allowed_symbols: [], validates_start_with_lowercase: true\"],[\"identifier_name\",\"(min_length) w/e: 3/2, (max_length) w/e: 40/60, excluded: [], allowed_symbols: [], validates_start_with_lowercase: true\"],[\"implicit_getter\",\"warning\"],[\"is_disjoint\",\"warning\"],[\"large_tuple\",\"warning: 2, error: 3\"],[\"leading_whitespace\",\"warning\"],[\"legacy_cggeometry_functions\",\"warning\"],[\"legacy_constant\",\"warning\"],[\"legacy_constructor\",\"warning\"],[\"legacy_nsgeometry_functions\",\"warning\"],[\"line_length\",\"warning: 120, error: 200, ignores urls: false, ignores function declarations: false, ignores comments: false\"],[\"mark\",\"warning\"],[\"multiple_closures_with_trailing_closure\",\"warning\"],[\"nesting\",\"(type_level) w: 1, (statement_level) w: 5\"],[\"notification_center_detachment\",\"warning\"],[\"opening_brace\",\"warning\"],[\"operator_whitespace\",\"warning\"],[\"private_over_fileprivate\",\"warning, validate_extensions: false\"],[\"private_unit_test\",\"[\"private_unit_test\",\"\",\"Unit test marked `private` will not be run by XCTest.\",\"XCTestCase\",\"\",\"warning\"]\"],[\"protocol_property_accessors_order\",\"warning\"],[\"redundant_discardable_let\",\"warning\"],[\"redundant_optional_initialization\",\"warning\"],[\"redundant_string_enum_value\",\"warning\"],[\"redundant_void_return\",\"warning\"],[\"return_arrow_whitespace\",\"warning\"],[\"shorthand_operator\",\"error\"],[\"statement_position\",\"(statement_mode) default, (severity) warning\"],[\"superfluous_disable_command\",\"error\"],[\"switch_case_alignment\",\"warning\"],[\"syntactic_sugar\",\"warning\"],[\"todo\",\"warning\"],[\"trailing_comma\",\"warning, mandatory_comma: false\"],[\"trailing_newline\",\"warning\"],[\"trailing_semicolon\",\"warning\"],[\"trailing_whitespace\",\"warning, ignores_empty_lines: false, ignores_comments: true\"],[\"type_body_length\",\"warning: 200, error: 350\"],[\"type_name\",\"(min_length) w/e: 3/0, (max_length) w/e: 40/1000, excluded: [], allowed_symbols: [], validates_start_with_lowercase: true\"],[\"unneeded_break_in_switch\",\"warning\"],[\"unused_closure_parameter\",\"warning\"],[\"unused_enumerated\",\"warning\"],[\"unused_optional_binding\",\"warning, ignore_optional_try: false\"],[\"valid_ibinspectable\",\"warning\"],[\"vertical_parameter_alignment\",\"warning\"],[\"vertical_whitespace\",\"warning, max_empty_lines: 1\"],[\"void_return\",\"warning\"],[\"weak_delegate\",\"warning\"],[\"xctfail_message\",\"warning\"]]]"

let dict = [k1: "test", k2: "test2"]
let data = try? JSONSerialization.data(withJSONObject: dict, options: [.prettyPrinted, .sortedKeys])

Expected Results:
data is not nil

Actual Results:
malloc: *** error for object 0x7ffeef219e30: Non-aligned pointer being freed (2)


Version:
10.13.3

Notes:

Comments

Still open on XCode 12

Still happens on XCode 12 / macOS 10.15.7 (19H2)


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!