TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Adding strlcpy() to glibc

67 pointsby corbetover 10 years ago

8 comments

nkurzover 10 years ago
Deep in the comments, &#x27;slibc&#x27; is mentioned. I hadn&#x27;t known about it, but this library provides str..._s() implementations of all the standard str...() functions as defined in Annex K of the C11 standard (which I also hadn&#x27;t known about).<p><a href="https://code.google.com/p/slibc/" rel="nofollow">https:&#x2F;&#x2F;code.google.com&#x2F;p&#x2F;slibc&#x2F;</a><p>At a glance, this seems like a better solution than any of alternatives: require the buffer length to always be specified, and call a user controlled handler instead of allowing an overflow. This handler defaults to abort().<p>While there are fine arguments to be made for using a real string library like bstring, are there downsides to the str_s approach compared to using the str, strp, or strl family of functions?
评论 #8364245 未加载
评论 #8365161 未加载
Zardoz84over 10 years ago
I usually use strncpy and enforce null at end, that virtually is the same that does a strlcpy.<p>But if you know how long is the source and destiny buffers (and if you are using str[nl]cpy, probably you know it), you could use memcpy and get a much more faster copy.
评论 #8364382 未加载
评论 #8366350 未加载
TazeTSchnitzelover 10 years ago
Safe string handling in C <i>can</i> be done, but not with char*.<p>PHP&#x27;s Zend Engine has safely-handled strings, for example, but we do that by reference-counting them and having an explicit length.
评论 #8364083 未加载
评论 #8364103 未加载
ape4over 10 years ago
Hopefully it will be added to the standard C library so it will be everywhere - eg Microsoft, Apple, etc.
评论 #8364241 未加载
评论 #8364049 未加载
评论 #8364059 未加载
评论 #8364349 未加载
penguindevover 10 years ago
I agree with drepper on this; it&#x27;s a solution in search of a problem. You should either know WTF you can accept or use a higher level construct that can resize. Silent truncation seems bad - truncation attacks are a real attack vector that SSL, for example, tries to prevent.
评论 #8364363 未加载
评论 #8364476 未加载
sigzeroover 10 years ago
I am curious why strlcpy() was not modified to check the length going in and out to check for truncation? I am not a C&#x2F;C++ guy but that is one question I had when he said that was one of the gripes for the function.
评论 #8364484 未加载
smegelover 10 years ago
&gt; The primary complaint about strlcpy() is that it gives no indication of whether the copied string was truncated or not.<p>So how hard would it be to add a return value indicating this?
评论 #8364672 未加载
评论 #8364959 未加载
评论 #8364721 未加载
bitwizeover 10 years ago
The correct approach is to use the strxxx_s family of functions. This is also in the most recent C standard.
评论 #8366371 未加载