5 ms·
Looks like Git has its own string type: https://github.com/git/git/blob/master/strbuf.h https://github.com/git/git/blob/master/strbuf.h https://github.com/git
by stiff 7y ago
Looks like Git has its own string type:
https://github.com/git/git/blob/master/strbuf.h https://github.com/git/git/blob/master/strbuf.h
https://github.com/git/git/blob/master/strbuf.c https://github.com/git/git/blob/master/strbuf.c
See this for the story of why strncpy/strncat are insecure:
https://en.wikipedia.org/wiki/C_string_handling#Replacements https://en.wikipedia.org/wiki/C_string_handling#Replacements
- adamnemecek 7y agoLike essentially all C programs.
- zsrxx 7y agoWhat's a good library for this kind of boilerplate? A lightweight one if possible, i.e. not fucking glib
- tlb 7y agoThere are far too many. APR is a solid choice -- very portable, well-tested, and fast. It uses memory pools a lot, which work well for many styles but not everything. See https://apr.apache.org/docs/apr/1.7/modules.html https://apr.apache.org/docs/apr/1.7/modules.html
- adamnemecek 7y agoRust. Half-joking.
- vmsp 7y agoSalvatore Sanfilippo, from Redis fame, has a nice one https://github.com/antirez/sds https://github.com/antirez/sds.
- zsrxx 7y agoI haven't looked much so I can't comment on the quality of the library but that hairdo is glorious. Props to Salvatore!
- megous 7y agoI'd rather make it explicit that returned/passed value is something other than a regular zero terminated string. Otherwise you can easilly make a mess, if you pass zero terminated string to a function that expects some special bytes in the memory before the pointer.
- chucksmash 7y agoThanks for linking this, it's always good to see how very strong engineers solve problems. Never in a million years would this approach have occurred to me. For the interested, the README in the linked repo is done well and clearly explains the approach itself as well as pros and cons. This also reminded me that I've been meaning to watch antirez' "Writing System Software"[0] videos on YouTube for quite a while now, so thanks for that too. [0]: https://www.youtube.com/playlist?list=PLrEMgOSrS_3fghr8ez63xKFNF-BA6Pz13 https://www.youtube.com/playlist?list=PLrEMgOSrS_3fghr8ez63x...
- doboyy 7y agoI do this, but I like to just return the actual struct and also avoid typedefing pointer semantics. Can double as a dynamic array too. It's true. We all have string libraries...
- CameronNemo 7y agolibbsd has strlcat and strlcpy. or just use snprintf everywhere.
- seamyb88 7y agoWhat's wrong with glib, other than not being lightweight? I love glib.
- emmelaich 7y agoThis is a good place to start: https://dwheeler.com/secure-programs/3.71/Secure-Programs-HOWTO/library-c.html https://dwheeler.com/secure-programs/3.71/Secure-Programs-HO...
- segfaultbuserr 7y agoEvery C programmer has his/her own standard library...
- r3trohack3r 7y agoI wonder about this every time I read C. There is talk about npm/rust having massive dependency trees (and they do) because it makes taking on dependencies too easy. But I feel like C is on the opposite side of the spectrum, where managing dependencies is difficult so every C code base is rolling it’s own version of everything. C also has an expansive standard library but it hasn’t offset re-invention of that stdlib in the ecosystem. I just see those implementations trapped inside code bases that don’t export them in a consumable way for other projects... Personally I love writing C but rarely do because dependency management makes innovation in those code bases so time consuming for me.
- segfaultbuserr 7y agoCreating a lightweight library would make C programming much more productive, and I'm sure many have tried, but I wonder why none of them see widespread applications, instead of writing some half-baked, ad-hoc helper functions. One reason, I guess, is the diverse range of applications of C, another reason is the lack of advanced features like generics and templates. But it would still be useful to create a simple library, intended for Unix-like platforms (beyond the BSD extension of stdlib). So it's more of a cultural problem?
- needs 7y agoIf I had to guess why there are no such lightweight library, it is because when somehow the library API doesn't provide what you want, you cannot hack the library itself easily. If you import the source code you may have trouble building it, and maintening such "patch" is not a lot of fun.
- segfaultbuserr 7y agoYes. I guess the fact C doesn't have advanced features like generics and templates probably have made the problem worse, making it difficult to create a "drop-in" API.
- commandlinefan 7y agoThat was my first thought. No strncpy? Ok, while (*s1 && n--) { *s2++ = *s1++; } *s2 = '\0'; it is, then.
- fwsgonzo 7y agoI don't think strncpy is the function you are looking for, either. Its for zeroing fixed-size character buffers. And it doesn't indicate problems. https://blog.liw.fi/posts/strncpy/ https://blog.liw.fi/posts/strncpy/
- jasonhansel 7y agoNice. Is there a similar standalone C library for safe/sane string handling? (Please don't tell me to use C++.)