4 ms·
It's not too hard if you are willing to use fixed length buffers and safe calls like: strlcpy, strlcat, and snprintf. Also not terribly hard to write a small a
by jhallenworld 11y ago
It's not too hard if you are willing to use fixed length buffers and safe calls like: strlcpy, strlcat, and snprintf.
Also not terribly hard to write a small arbitrary length string library as part of the project.
- jheriko 11y agoits still not as easy as in languages with modern libraries. a = "foo" + "bar"; is very useful so is b = a.split( "," ); or a = b.trim(); etc. C strings are rarely so convenient. its more obvious with the more high level things you want. to do something like b = a.split( "," ); on a string that is not know until runtime you need dynamic allocations and you could use strtok to fill that memory with the results, but its an error-prone ballache by comparison.
- krapp 11y agoSomeone should totally build a dynamically typed language around C which is specifically meant for dealing with HTTP and serving HTML.
- kbenson 11y agoI still maintain that + is a terrible operator for concatenating strings, and results in quite a few types of bugs that are pervasive in many languages (usually from trying to automatically coerce non-matching operands).
- vardump 11y agoNot all implementations of snprintf behave the same way. Many are subtly broken. Some don't nul terminate the string if it's longer than the destination buffer. Also the return value can be -1 (correct behavior on failure, but it's often left unchecked) or more than destination buffer size! Fun times if you accumulate total written without range checking snprintf return value...
- vardump 11y agoTo add, notably this: _snprintf, _snprintf_l, _snwprintf, _snwprintf_l -> https://msdn.microsoft.com/en-us/library/2ts7cx93.aspx https://msdn.microsoft.com/en-us/library/2ts7cx93.aspx "If len > count, count characters are stored in buffer, no null-terminator is appended, and a negative value is returned." Uh, what? I guess that's why it is _snprintf and not snprintf, huh? They do provide secure [1] versions too, but you need to be careful which one you pick. Those secure versions terminate execution in some cases if buffer length is exceeded. [1]: https://msdn.microsoft.com/en-us/library/f30dzcf6.aspx https://msdn.microsoft.com/en-us/library/f30dzcf6.aspx - better remember that _TRUNCATE flag.