Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork939
Commit9e86053
committed
Replace the one use of mktemp in the git module
This makes two related changes to git.index.util.TemporaryFileSwap:- Replace mktemp with mkstemp and then immediately closing the file. This avoids two possible name clashes: the usual name clash where the file may be created by another process between when mktemp generates the name and when the file is created, and the problem that mktemp does not check for files that contain the generated name as a part. The latter is specific to the use here, where a string generated by mktemp was manually concatenated to the base filename. This addresses that by passing the filename as the prefix for mkstemp to use.- Replace os.remove with os.replace and simplify. This is made necessary on Windows by the file already existing, due to mkstemp creating it. Deleting the file and allowing it to be recreated would reproduce the mktemp race condition in full (obscured and with extra steps). But os.replace supports an existing target file on all platforms. It is now also used in __exit__, where it allows the Windows check for the file to be removed, and (in any OS) better expresses the intent of the code to human readers. Furthermore, because one of the "look before you leap" checks in __exit__ is removed, the minor race condition in cleaning up the file is somewhat decreased.A small amount of related refactoring is included. The interface isnot changed, even where it could be simplified (by letting __exit__return None), and resource acquisition remains done on constructionrather than in __enter__, because changing those aspects of thedesign could break some existing uses.Although the use of mktemp replaced here was in the git module andnot the test suite, its use was to generate filenames for use in adirectory that would ordinarily be under the user's control, suchthat the ability to carry out typical mktemp-related attacks wouldalready require the ability to achieve the same goals more easilyand reliably. Although TemporaryFileSwap is a public class that maybe used directly by applications, it is only useful for making atemporary file in the same directory as an existing file. Thus theintended benefits of this change are mainly to code quality anda slight improvement in robustness.1 parent41fac85 commit9e86053
1 file changed
+6
-10
lines changedLines changed: 6 additions & 10 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
3 | 3 |
| |
4 | 4 |
| |
5 | 5 |
| |
| 6 | + | |
6 | 7 |
| |
7 | 8 |
| |
8 | 9 |
| |
| |||
40 | 41 |
| |
41 | 42 |
| |
42 | 43 |
| |
43 |
| - | |
44 |
| - | |
45 |
| - | |
46 |
| - | |
47 |
| - | |
48 |
| - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
49 | 48 |
| |
50 | 49 |
| |
51 | 50 |
| |
| |||
57 | 56 |
| |
58 | 57 |
| |
59 | 58 |
| |
60 |
| - | |
61 |
| - | |
62 |
| - | |
63 |
| - | |
| 59 | + | |
64 | 60 |
| |
65 | 61 |
| |
66 | 62 |
| |
|
0 commit comments
Comments
(0)