This is an archive of the discontinued LLVM Phabricator instance.

Fix memory leaks of buffer allocated in __kmp_str_format()
ClosedPublic

Authored by vhscampos on Nov 16 2016, 6:45 PM.

Details

Summary

Following comment in kmp_str.c line 480, strings allocated by kmp_str_format() have to be freed using kmp_str_free(). In many instances, these buffers were freed by calling free() directly.

In almost all calls to kmp_msg(), KMP_ERR and KMP_SYSERRCODE are used to allocate messages. This allocation uses kmp_str_format() as well, but the buffer is not freed afterwards. I made changes aiming to eliminate these leaks.

Diff Detail

Repository
rL LLVM

Event Timeline

vhscampos updated this revision to Diff 78303.Nov 16 2016, 6:45 PM
vhscampos retitled this revision from to Fix memory leaks of buffer allocated in __kmp_str_format().
vhscampos updated this object.
vhscampos added a subscriber: openmp-commits.
AndreyChurbanov requested changes to this revision.Nov 17 2016, 6:17 AM
AndreyChurbanov edited edge metadata.
  1. __kmp_msg(kmp_ms_fatal,...) never returns, and it cleans up its arguments inside. Thus trying to clean its argument once more time is an error.
  2. __kmp_msg(kmp_ms_warning,...) cleans its arguments in case it tries to print anything, and doesn't clean in case it returns immediately. Thus the added cleanup action should be conditional, e.g.
if(__kmp_generate_warnings == kmp_warnings_off)
    __kmp_str_free(&err_code.str);

I tried to mark all changes those need to be under condition, and those are OK. All the changes for fatal messages should be reverted.

runtime/src/kmp_affinity.h
61 ↗(On Diff #78303)

This change is bad, as well as all other changes for __kmp_msg(kmp_ms_fatal,...).

runtime/src/kmp_environment.c
583 ↗(On Diff #78303)

This change is OK, as the __kmp_str_free frees and nullifies parameter.

runtime/src/kmp_i18n.c
160 ↗(On Diff #78303)

Needs condition.

424 ↗(On Diff #78303)

Needs condition.

827 ↗(On Diff #78303)

OK to remove the comment.

835 ↗(On Diff #78303)

I think keeping all declarations together looks better that moving one here. Why this change?

938 ↗(On Diff #78303)

These two changes are OK.

964 ↗(On Diff #78303)

These two are OK as well.

runtime/src/kmp_itt.c
110 ↗(On Diff #78303)

Needs condition.

115 ↗(On Diff #78303)

Needs condition.

139 ↗(On Diff #78303)

Needs condition.

146 ↗(On Diff #78303)

Needs condition.

runtime/src/kmp_settings.c
363 ↗(On Diff #78303)

This is OK.

421 ↗(On Diff #78303)

These two are OK.

2238 ↗(On Diff #78303)

This is OK.

runtime/src/kmp_str.c
423 ↗(On Diff #78303)

This is OK.

481 ↗(On Diff #78303)

OK

runtime/src/z_Linux_util.c
175 ↗(On Diff #78303)

Needs condition.

205 ↗(On Diff #78303)

Needs condition.

255 ↗(On Diff #78303)

Needs condition.

288 ↗(On Diff #78303)

Needs condition.

732 ↗(On Diff #78303)

Needs condition.

989 ↗(On Diff #78303)

Needs condition.

1097 ↗(On Diff #78303)

Needs condition.

1166 ↗(On Diff #78303)

Needs condition.

runtime/src/z_Windows_NT_util.c
570 ↗(On Diff #78303)

Needs condition.

This revision now requires changes to proceed.Nov 17 2016, 6:17 AM
vhscampos marked 25 inline comments as done.Nov 17 2016, 11:11 AM

Ok, I'm fixing the patch. Thanks.

vhscampos updated this revision to Diff 78395.Nov 17 2016, 11:14 AM
vhscampos edited edge metadata.

Applied your comments.

AndreyChurbanov requested changes to this revision.Nov 21 2016, 8:00 AM
AndreyChurbanov edited edge metadata.
AndreyChurbanov added inline comments.
runtime/src/kmp_i18n.c
151 ↗(On Diff #78395)

Something is wrong with indents here. Please check that replacement of tabs with spaces done correctly in this code block.

428 ↗(On Diff #78395)

It should be err_msg.str variable, not err_code.str. Or, alternatively, name it err_code in lines 419, 423 and here.

This revision now requires changes to proceed.Nov 21 2016, 8:00 AM
vhscampos updated this revision to Diff 78750.Nov 21 2016, 12:03 PM
vhscampos edited edge metadata.
vhscampos marked 2 inline comments as done.

Fixed indentation and variable name issues.

AndreyChurbanov accepted this revision.Nov 22 2016, 1:26 AM
AndreyChurbanov edited edge metadata.

LGTM

This revision is now accepted and ready to land.Nov 22 2016, 1:26 AM

Victor,
shell I commit the change on your behalf?

This revision was automatically updated to reflect the committed changes.