FastLED 3.10.6
Loading...
Searching...
No Matches

◆ strncpy()

char * fl::strncpy ( char * dest,
const char * src,
size_t n )

Definition at line 50 of file cstring.cpp.hpp.

50 {
51 // Unlike ::strncpy, this ALWAYS null-terminates: it copies at most n
52 // characters from src, then writes a terminator at dest[copied]
53 // (copied <= n). Callers pass n = capacity - 1 (dest sized >= n+1),
54 // so the terminator is always in bounds. This closes the classic
55 // strncpy hazard where a source of length >= n leaves dest
56 // unterminated and downstream string walks run off the buffer
57 // (FastLED#3588: an unterminated HTTP request URI let a query-strip
58 // loop write a stray null byte one past a heap allocation).
59 size_t i = 0;
60 for (; i < n && src[i] != '\0'; ++i) {
61 dest[i] = src[i];
62 }
63 dest[i] = '\0';
64 // Preserve ::strncpy's zero-padding of the remaining destination
65 // bytes so callers relying on that behavior are unaffected.
66 for (size_t j = i + 1; j < n; ++j) {
67 dest[j] = '\0';
68 }
69 return dest;
70}

References FL_NO_EXCEPT.