Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
downloader prepare_next_transfer: make cleanup on error... less error…
…-prone @cgwalters suggested the cleanup attribute could be used to make sure we don't leak resources on one of the error paths, as this was overlooked in the past. #77 (comment) Sadly it's not straightforward. Two local variables (h, f) are actually saved into `target`, and should not be cleaned up on success. A simple solution is a `goto fail` which cleans the copies in `target`. The local variables are considered as references into `target`. We use the same null checks that auto-cleanup would. So now every error path uses the same `goto fail`, and each variable is cleaned up in exactly one place. Hence we reduce the complexity and potential for error. One purely local variable, `full_url`, can be handled with auto-cleanup. Finally we have `f = fdopen(fd)`. On failure, we need to cleanup fd (oops) On success, fd is owned by target->f, and we must *not* free it. And we still need access to `fd` for some further operations. We can at least move this tricky detail into a small, well-defined subroutine. An alternative would have been to handle all variables with auto-cleanup, treat the saved values as moved, and re-order the saves so they came after all possible failures. I decided against this because I felt the last of these requirements would not be explicitly visible.
- Loading branch information
1 parent
f5deb8e
commit 3549e61
Showing
1 changed file
with
74 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters