summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_string.h
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@nginx.com>2022-10-25 18:42:52 +0200
committerAlejandro Colomar <alx@nginx.com>2022-11-16 13:02:04 +0100
commit647c349be1235ce9f43bb53d30e21f4e6dae0bd3 (patch)
treeaf2782b4c37a0c86a080ae9d3775d54c87fe5c41 /src/nxt_string.h
parente957ddf43d32e8e6d8778c3dc34c50d7f66c5eab (diff)
downloadunit-647c349be1235ce9f43bb53d30e21f4e6dae0bd3.tar.gz
unit-647c349be1235ce9f43bb53d30e21f4e6dae0bd3.tar.bz2
Added nxt_ustr2str() to make C strings from fixed-width buffers.
This function makes it easy to transform a fixed-width buffer (which is how we represent strings in Unit most of the time) into a proper C string (NUL-terminated). We need to do this when interfacing libraries or the kernel, where most APIs expect NUL-terminated strings. The implementation is similar to strncpy_s(3), but avoids the unnecessary runtime checks. It's better to wrap the function in a macro and do as many static_assert(3)s as one considers necessary; in fact, if in the future C allows backwards VLA syntax, static analysis could be better than those static_assert(3)s. We use char for NUL-terminated strings, and u_char for the *u*nterminated strings. The documentation for the function: /* * SYNOPSIS * void ustr2str(char dst[restrict .n+1], * const u_char src[restrict .n], * size_t n); * * ARGUMENTS * dst Pointer to the first byte of the destination buffer. * src Pointer to the first byte of the source string. * n Size of 'src'. * * DESCRIPTION * Copy a string from the fixed-width source string, which may be * not-NUL-terminated, into a NUL-terminated string in the * destination buffer. * * CAVEATS * If the destination buffer is not wider than the source buffer * at least by 1 byte, the behavior is undefined. * * Use of this function normally indicates a problem in the design * of the strings, since normally it's better to guarantee that all * strings are properly terminated. The main use for this function * is to interface with some standard buffers, such as those * defined in utmp(7), which for historical reasons are not * guaranteed to be terminated. * * EXAMPLES * u_char src[10] = "0123456789"; // not NUL-terminated * char dst[sizeof(src) + 1]; * * static_assert(lengthof(src) < lengthof(dst)) * ustr2str(dst, src, lengthof(src)); * * SEE ALSO * nxt_sts2str(3), strlcpy(3), strscpy(9) */ Cc: Andrew Clayton <a.clayton@nginx.com> Signed-off-by: Alejandro Colomar <alx@nginx.com>
Diffstat (limited to '')
-rw-r--r--src/nxt_string.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/nxt_string.h b/src/nxt_string.h
index 3e4a7be2..62459987 100644
--- a/src/nxt_string.h
+++ b/src/nxt_string.h
@@ -120,6 +120,9 @@ typedef struct {
} while (0)
+NXT_EXPORT inline void nxt_ustr2str(char *restrict dst,
+ const u_char *restrict src, size_t length);
+
NXT_EXPORT nxt_str_t *nxt_str_alloc(nxt_mp_t *mp, size_t length);
NXT_EXPORT nxt_str_t *nxt_str_dup(nxt_mp_t *mp, nxt_str_t *dst,
const nxt_str_t *src);
@@ -168,4 +171,12 @@ NXT_EXPORT ssize_t nxt_base64_decode(u_char *dst, u_char *src, size_t length);
extern const uint8_t nxt_hex2int[256];
+inline void
+nxt_ustr2str(char *restrict dst, const u_char *restrict src, size_t length)
+{
+ memcpy(dst, src, length);
+ dst[length] = '\0';
+}
+
+
#endif /* _NXT_STRING_H_INCLUDED_ */