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

Add nil check in code-gen converter for binary #693

Merged
merged 3 commits into from Mar 26, 2020

Conversation

chengs041021
Copy link
Contributor

@chengs041021 chengs041021 commented Mar 25, 2020

The problem:

Converter doesn't check nil for binary type. So when it tries to convert binary to a nested field, for example out.NestedTwo.Two, it can cause NPEs. Without this fix, it can generate code like the following, while out.NestedTwo can be nil.

out.NestedTwo.Two = []byte(in.Two)

The fix:

Add nil check for binary type.

Testing:

Added test, failed and got:

out.One = (*string)(in.One)
out.NestedTwo.NestedNestedTwo.Two = []byte(in.Two)

Added code, test passed and got

out.One = (*string)(in.One)
if out.NestedTwo == nil {
out.NestedTwo = &structs.NestedBar{}
}
if out.NestedTwo.NestedNestedTwo == nil {
out.NestedTwo.NestedNestedTwo = &structs.NestedNestedBar{}
}
out.NestedTwo.NestedNestedTwo.Two = []byte(in.Two)

@CLAassistant
Copy link

CLAassistant commented Mar 25, 2020

CLA assistant check
All committers have signed the CLA.

@coveralls
Copy link

coveralls commented Mar 25, 2020

Coverage Status

Coverage increased (+0.005%) to 69.829% when pulling aa38399 on sc-binary-check-nil into 993a605 on master.

@abhishekparwal
Copy link
Contributor

Otherwise it generates code like
, where it will always NPE at L213 because out.Request is always empty.

To verify, I:
Added test, failed and got:

out.One = (*string)(in.One)
out.NestedTwo.NestedNestedTwo.Two = []byte(in.Two)

Added code, test passed and got

out.One = (*string)(in.One)
if out.NestedTwo == nil {
out.NestedTwo = &structs.NestedBar{}
}
if out.NestedTwo.NestedNestedTwo == nil {
out.NestedTwo.NestedNestedTwo = &structs.NestedNestedBar{}
}
out.NestedTwo.NestedNestedTwo.Two = []byte(in.Two)

removed proprietary link, please describe the problem in a general way and not using specific proprietary links.

Copy link
Contributor

@abhishekparwal abhishekparwal left a comment

Choose a reason for hiding this comment

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

make a change for nits and description of the bug. approving it.

@@ -808,6 +808,9 @@ func (c *TypeConverter) genStructConverter(
}
case *compile.BinarySpec:
// TODO: handle override. Check if binarySpec can be optional.
for _, line := range checkOptionalNil(indent, c.uninitialized, toIdentifier, prevKeyPrefixes, c.useRecurGen) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: remove a todo above since its now handled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@chengs041021 chengs041021 merged commit ab8c042 into master Mar 26, 2020
@chengs041021 chengs041021 deleted the sc-binary-check-nil branch March 26, 2020 17:24
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.

None yet

4 participants