<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>Le 25 mars 2010 à 00:18, David Chisnall a écrit :</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>On 24 Mar 2010, at 23:02, Jean-Daniel Dupas wrote:<br><br><blockquote type="cite">I see a security warning in the analyzer that saids that using mktemp is always wrong. <br></blockquote><blockquote type="cite">I though using mktemp + open(path, O_CREAT | O_EXCL) was safe.<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Did I miss something, or is the analyzer a little too much strict about this function ? <br></blockquote><br>This is moderately safe, but you have to make sure that you check the open() doesn't fail.  You end up reimplementing mkstemp(), which is pretty much guaranteed to be the wrong thing to do in any situation.<br><br><blockquote type="cite">I known that mkstemp should be preferred, but some API (like SQLite) provide only an "open" like function, and no "fdopen" like function, so mkstemp is useless in these case.<br></blockquote><br>mkstemp isn't useless in these cases.  You can use mkstemp like this:<br><br>char *name = strdup("templateXXX");<br>close(mkstemp(name));<br>custom_open_function(name);<font class="Apple-style-span" color="#000000"><font class="Apple-style-span" color="#144FAE"><br></font></font></div></blockquote><div><br></div>Thanks, I will try this.<br><br><blockquote type="cite"><div>If you are doing two-stage creation with a more complex API, you should use mkdtemp() and then create the temporary file inside the newly created temporary directory.<br><br>Note that the correct way of creating a temporary database with sqlite is to pass the empty string to sqlite3_open() and not use mk[s]temp() at all.  This will also delete the database for you automatically when you close it.<br></div></blockquote><div><br></div><div>Not an option in my case.</div><div>I'm using sqlite as a document file format, and I want to implement a "safe save as" function that do not override an existing file if it failed to save the document.</div><div>So I'm creating a temporary base, I populate it and close it, and then I use exchangedata(2) (atomically exchange data between two files on Darwin) to replace the existing file.</div><div>So I must have control over which file system the file is created (it must be the same than the target file), and also, I don't want that the file be deleted on close.</div><div><br></div><blockquote type="cite"><div>David<br></div></blockquote></div><br><div>
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div>-- Jean-Daniel</div><div><br></div><div><br></div></span><br class="Apple-interchange-newline">
</div>
<br></body></html>