summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@nginx.com>2022-10-25 18:42:52 +0200
committerAlejandro Colomar <alx@nginx.com>2022-11-15 13:11:15 +0100
commit35e72584b94f07c121fbfa3a366b771e868ee786 (patch)
tree6b6393d76b4395efbb7ad3b9085cb7b2bf0c73ec /src
parentbea5bc98e31d2367c6ae4e16279a206df494ae4a (diff)
downloadunit-35e72584b94f07c121fbfa3a366b771e868ee786.tar.gz
unit-35e72584b94f07c121fbfa3a366b771e868ee786.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 'src')
-rw-r--r--src/nxt_string.c4
-rw-r--r--src/nxt_string.h11
2 files changed, 15 insertions, 0 deletions
diff --git a/src/nxt_string.c b/src/nxt_string.c
index 1ca595a1..39f472e9 100644
--- a/src/nxt_string.c
+++ b/src/nxt_string.c
@@ -7,6 +7,10 @@
#include <nxt_main.h>
+extern inline void nxt_ustr2str(char *restrict dst, const u_char *restrict src,
+ size_t length);
+
+
nxt_str_t *
nxt_str_alloc(nxt_mp_t *mp, size_t length)
{
diff --git a/src/nxt_string.h b/src/nxt_string.h
index c27ff63d..275a62ee 100644
--- a/src/nxt_string.h
+++ b/src/nxt_string.h
@@ -58,6 +58,9 @@ struct nxt_str_s {
};
+NXT_EXPORT inline void nxt_ustr2str(char *restrict dst,
+ const u_char *restrict src, size_t length);
+
NXT_EXPORT void nxt_memcpy_lowcase(u_char *dst, const u_char *src,
size_t length);
NXT_EXPORT void nxt_memcpy_upcase(u_char *dst, const u_char *src,
@@ -171,4 +174,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_ */