Comments (3)
Here is my safer strncpy, verbatim paste from my core lib. If you want to adapt or use any part of this, I release it under the public domain.
The FTG_ATTRIBUTES() thing is a macro that generates a compiler warning if the truncation bit is ignored. Since it is a potential security risk if a string is truncated, I force a check. This is appropriate for my code. (I wish I could conditionally avoid this warning if *src was a literal).
/* Fill up to max_copy characters in dst, including null. Unlike strncpy(), a null
terminating character is guaranteed to be appended, EVEN if it overwrites
the last character in the string.
Only appends a single NULL character instead of NULL filling the string. The
trailing bytes are left uninitialized.
No bytes are written if max_copy is 0, and FTG_ASSERT is thrown.
\return 1 on truncation or max_copy==0, zero otherwise.
*/
FTG_ATTRIBUTES(FTG_EXT_warn_unused_result) int
ftg_strncpy(char *ftg_restrict dst, const char *ftg_restrict src, size_t max_copy)
{
size_t n;
char *d;
FTG_ASSERT(dst);
FTG_ASSERT(src);
FTG_ASSERT(max_copy > 0);
if (max_copy == 0)
return 1;
n = max_copy;
d = dst;
while ( n > 0 && *src != '\0' )
{
*d++ = *src++;
--n;
}
/* Truncation case -
terminate string and return true */
if ( n == 0 )
{
dst[max_copy-1] = '\0';
return 1;
}
/* No truncation. Append a single NULL and return. */
*d = '\0';
return 0;
}
from utf8.h.
I'll take your comments on restrict on board for sure - I'm not certain I like the assert myself personally (but I'm willing to spend some brain cycles on the thought before I commit one way or the other!)
Thanks for the input though - all comments are good comments 😄
from utf8.h.
Just for clarity - I've decided against adding the assert, but I have done the restrict change. Thanks again for spending time reviewing my lib!
from utf8.h.
Related Issues (20)
- Possibility of dual-licensing? HOT 5
- `utf8nvalid` reads out bounds HOT 2
- strn*/utf8n* functions HOT 5
- utf8upr/lwr size issues? HOT 3
- provide get codepoint visual width function HOT 1
- utf8ncpy incorrectly removes last valid codepoint HOT 7
- Add test similar to one used in issue 109
- support utf8 convert to utf16? HOT 1
- utf8rchr issue HOT 3
- utf8makevalid read out of bounds (+ other functions) HOT 1
- utf8makevalid : test to identify sequence length and possible values not sufficient HOT 1
- grapheme support HOT 1
- Not an issue HOT 1
- Allow programmer specified allocator HOT 2
- utf8valid with size HOT 1
- utf8tok and utf8tok_r HOT 2
- Support constexpr? HOT 3
- clang-format? HOT 1
- utf8ncat - size wraparound bug HOT 1
- Way of removing malloc completely HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from utf8.h.