Nico Rieck
2013-Jul-23 16:48 UTC
[LLVMdev] [Patch] WinCOFFObjectWriter: fix for storing pointer to string table in header name field
On 23.07.2013 18:43, Reid Kleckner wrote:> Is there a problem if the string is not null terminated? If not, you can > snprintf it right into place instead of doing sprintf+mempcy.snprintf always null-terminates (and truncates if there's not enough space). -Nico
Nico Rieck
2013-Jul-23 16:55 UTC
[LLVMdev] [Patch] WinCOFFObjectWriter: fix for storing pointer to string table in header name field
On 23.07.2013 18:48, Nico Rieck wrote:> On 23.07.2013 18:43, Reid Kleckner wrote: >> Is there a problem if the string is not null terminated? If not, you can >> snprintf it right into place instead of doing sprintf+mempcy. > > snprintf always null-terminates (and truncates if there's not enough > space). Urgh, nevermind. Brain fart here. -Nico
Reid Kleckner
2013-Jul-23 16:55 UTC
[LLVMdev] [Patch] WinCOFFObjectWriter: fix for storing pointer to string table in header name field
On Tue, Jul 23, 2013 at 12:48 PM, Nico Rieck <nico.rieck at gmail.com> wrote:> On 23.07.2013 18:43, Reid Kleckner wrote: > >> Is there a problem if the string is not null terminated? If not, you can >> snprintf it right into place instead of doing sprintf+mempcy. >> > > snprintf always null-terminates (and truncates if there's not enough > space).Nuh uh: "The _snprintf function formats and stores count or fewer characters in buffer, and appends a terminating null character if the formatted string length is strictly less than count characters." http://msdn.microsoft.com/en-us/library/2ts7cx93(v=vs.100).aspx Please don't assume snprintf always null terminates. This may be Windows-specific behavior that you shouldn't rely on. If that's the case, ignore my suggestion. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130723/bd7701ac/attachment.html>
Nico Rieck
2013-Jul-23 17:10 UTC
[LLVMdev] [Patch] WinCOFFObjectWriter: fix for storing pointer to string table in header name field
On 23.07.2013 18:55, Reid Kleckner wrote:> Please don't assume snprintf always null terminates.I'm reading C99 7.19.6.4 and C11 7.21.6.5 which says: "If n is zero, nothing is written, and s may be a null pointer. Otherwise, output characters beyond the n-1st are discarded rather than being written to the array, and a null character is written at the end of the characters actually written into the array." and "Thus, the null-terminated output has been completely written if and only if the returned value is nonnegative and less than n." -Nico
Richard
2013-Jul-23 18:51 UTC
[LLVMdev] [Patch] WinCOFFObjectWriter: fix for storing pointer to string table in header name field
In article <CACs=tyLqAn7GNNobgwr+K96k-Zw1y5N16dzOeOB2O8OtrHyNig at mail.gmail.com>, Reid Kleckner <rnk at google.com> writes:> Nuh uh: "The _snprintf function formats and stores count or fewer > characters in buffer, and appends a terminating null character if the > formatted string length is strictly less than count characters." > http://msdn.microsoft.com/en-us/library/2ts7cx93(v=vs.100).aspx > > Please don't assume snprintf always null terminates. > > This may be Windows-specific behavior that you shouldn't rely on. If > that's the case, ignore my suggestion.Yes, _snprintf with MSVC is not a conforming C99 (or whatever) standard implementation of snprintf. I've filed a bug on it, but don't expect a fix from them anytime soon as they deem it low priority. This is just another reason to stay away from these error-prone C APIs in C++ code. -- "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline> The Computer Graphics Museum <http://computergraphicsmuseum.org> The Terminals Wiki <http://terminals.classiccmp.org> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
Possibly Parallel Threads
- [LLVMdev] [Patch] WinCOFFObjectWriter: fix for storing pointer to string table in header name field
- [LLVMdev] [Patch] WinCOFFObjectWriter: fix for storing pointer to string table in header name field
- [LLVMdev] [Patch] WinCOFFObjectWriter: fix for storing pointer to string table in header name field
- [LLVMdev] [Patch] WinCOFFObjectWriter: fix for storing pointer to string table in header name field
- Link to xva.py broken