Skip to content

fix potential memory leaks in fileio.c and semantics.c - #199

Merged
mikkelfj merged 1 commit into
dvidelabs:masterfrom
Adsun701:fix-potential-memory-leaks
Jul 22, 2021
Merged

mikkelfj merged 1 commit into
dvidelabs:masterfrom
Adsun701:fix-potential-memory-leaks

Conversation

@Adsun701

Copy link
Copy Markdown
Contributor

This fixes potential memory leaks that were detected by a quick run of cppcheck. In fileio.c, memcpy() will only copy from prefix to path if prefix_len is greater than 0, so copying from a potentially null pointer is avoided. In semantics.c, index.len is set accordingly based on index.type so that the former variable will always be initialized.

@mikkelfj

Copy link
Copy Markdown
Contributor

Copy operation: yes, that is technically undefined behaviour for null pointers regardless of length. Fine to fix though it hardly matters in praxis.

index: I don't recall what the len field is used for - but is not used with coerce. I'm not sure if it semantically makes sense to assing a length for numerics if the length field is not set elsewhere for numerics.

However, the index value ought to be zero initialized.

@Adsun701
Adsun701 force-pushed the fix-potential-memory-leaks branch from 1bf46ef to aaf76bb Compare July 22, 2021 16:55
@Adsun701

Copy link
Copy Markdown
Contributor Author

@mikkelfj Fixed with index.len set to 0.

@mikkelfj

Copy link
Copy Markdown
Contributor

I just researched in parser.c. It seems that the v->len is only used with type information where scalar types have length 1 and fixed length arrays can have other lengths. For normal integer parsing, the length field is not used and most likely defaults to zero. I think the length field was added relatively late to handle fixed length arrays.

Thus, your latest change is fine, but I think leaving the code as it were is more correct if you initialize with fb_value_t index = { 0 };.

@Adsun701
Adsun701 force-pushed the fix-potential-memory-leaks branch from aaf76bb to 1f79d86 Compare July 22, 2021 17:18
@Adsun701

Copy link
Copy Markdown
Contributor Author

@mikkelfj Fixed, thanks for the input.

@mikkelfj
mikkelfj merged commit 5bc7c35 into dvidelabs:master Jul 22, 2021
@mikkelfj

Copy link
Copy Markdown
Contributor

Thanks

@Adsun701
Adsun701 deleted the fix-potential-memory-leaks branch July 22, 2021 18:33
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