Skip to content

jsonbuilder: attempt to handle memory allocation errors - v5#8916

Closed
jasonish wants to merge 2 commits into
OISF:masterfrom
jasonish:jsonbuilder-alloc/v5
Closed

jsonbuilder: attempt to handle memory allocation errors - v5#8916
jasonish wants to merge 2 commits into
OISF:masterfrom
jasonish:jsonbuilder-alloc/v5

Conversation

@jasonish
Copy link
Copy Markdown
Member

Ticket: https://redmine.openinfosecfoundation.org/issues/6057
Previous PR: #8855

Changes from last PR:

  • Fix per-commit check
  • Remove base64 update, will do separate PR

jasonish added 2 commits May 24, 2023 16:43
Some very minor changes to formatting.
Use try_reserve before growing the internal buffer, and the internal
state vector. This allows allocation errors to be caught and an error
returned instead of just aborting the process.

Ticket: OISF#6057
@codecov
Copy link
Copy Markdown

codecov Bot commented May 25, 2023

Codecov Report

Merging #8916 (c29a728) into master (ebe0a7b) will increase coverage by 0.04%.
The diff coverage is 86.97%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8916      +/-   ##
==========================================
+ Coverage   82.30%   82.34%   +0.04%     
==========================================
  Files         969      969              
  Lines      273335   273376      +41     
==========================================
+ Hits       224961   225111     +150     
+ Misses      48374    48265     -109     
Flag Coverage Δ
fuzzcorpus 64.76% <78.36%> (+0.10%) ⬆️
suricata-verify 60.46% <80.11%> (+<0.01%) ⬆️
unittests 62.95% <66.97%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

@suricata-qa
Copy link
Copy Markdown

Information: QA ran without warnings.

Pipeline 14035

Comment thread rust/src/jsonbuilder.rs
}

fn encode_base64(&mut self, val: &[u8]) -> Result<&mut Self, JsonError> {
let encoded_len = ((val.len() / 4) * 3) + 2;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong : if val.len() == 1, this gives 2 instead of 4

This should rather be 4*((val.len()+2)/3), no ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment thread rust/src/jsonbuilder.rs
/// than building onto the buffer.
///
/// TODO: Revisit this, would be nice to build directly onto the
/// existing buffer.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is worth revisiting this part.
Note that std::str::from_utf8 error case seems unreachable (as you encode the string correctly directly), so you should be able to push onto the self.buf directly

Copy link
Copy Markdown
Contributor

@catenacyber catenacyber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still the base64 length looks suspicious + encode_string could/should be simplified

@jasonish
Copy link
Copy Markdown
Member Author

Still the base64 length looks suspicious + encode_string could/should be simplified

I feel touching encode_string here is out of scope, unless the changes added within scope are wrong.

@catenacyber
Copy link
Copy Markdown
Contributor

Still the base64 length looks suspicious + encode_string could/should be simplified

I feel touching encode_string here is out of scope, unless the changes added within scope are wrong.

I think you can have simpler changes with refactoring the encode_string code, which makes it easier to check that all allocations are checked, but I leave you final judge

@jasonish
Copy link
Copy Markdown
Member Author

Encoded length fixed here: #8927

@jasonish jasonish closed this May 25, 2023
@jasonish jasonish deleted the jsonbuilder-alloc/v5 branch May 31, 2023 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants