Skip to content
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

Fix undefined behavior when using CLI's process command. #307

Merged
merged 1 commit into from
Aug 23, 2022

Conversation

nirosys
Copy link
Contributor

@nirosys nirosys commented Jul 22, 2022

Issue #, if available: None

Description of changes:
When using the process subcommand of the Ion-C CLI the ion_cli_command_process function is called using NULL for output ION_STRING parameter. At the end of ion_cli_command_process as part of the function's cleanup it calls ion_cli_close_writer supplying the NULL output string, which then gets used in a call to ion_event_writer_close, supplying the value and length fields of the ION_STRING to capture the writer's output.

Since the output string is NULL, the dereferences to obtain the value and length fields are undefined behavior. ub-san identifies the issue when running a debug build like so:

$ echo '{foo: bar}' | tools/cli/ion process -f pretty -
/home/ubuntu/ion-c/tools/cli/cli.cpp:236:5: runtime error: member access within null pointer of type 'ION_STRING' (aka '_ion_string')
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/ubuntu/ion-c/tools/cli/cli.cpp:236:5 in 
/home/ubuntu/ion-c/tools/cli/cli.cpp:236:5: runtime error: member access within null pointer of type 'ION_STRING' (aka '_ion_string')
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/ubuntu/ion-c/tools/cli/cli.cpp:236:5 in 
{
  foo: bar
}

This PR simply adjusts the call site to supply a NULL for each, if output is NULL. Since we're writing directly to stdout, the output type is IO_TYPE_CONSOLE, ensuring that nothing will be done with the output value pointer and length.

After the PR:

$ echo '{foo: bar}' | tools/cli/ion process -f pretty -
{
  foo: bar
}

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@nirosys nirosys marked this pull request as ready for review July 22, 2022 20:38
@nirosys nirosys merged commit ec9e060 into amazon-ion:master Aug 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants