2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 Optimized string handling code, including c11-compliant
41 "safe C library" variants.
44 #ifndef included_clib_string_h
45 #define included_clib_string_h
47 #include <vppinfra/clib.h> /* for CLIB_LINUX_KERNEL */
48 #include <vppinfra/vector.h>
50 #ifdef CLIB_LINUX_KERNEL
51 #include <linux/string.h>
58 #ifdef CLIB_STANDALONE
59 #include <vppinfra/standalone_string.h>
63 #include <x86intrin.h>
66 /* Exchanges source and destination. */
67 void clib_memswap (void *_a, void *_b, uword bytes);
70 * the vector unit memcpy variants confuse coverity
71 * so don't let it anywhere near them.
75 #include <vppinfra/memcpy_avx512.h>
77 #include <vppinfra/memcpy_avx2.h>
79 #include <vppinfra/memcpy_sse3.h>
81 #define clib_memcpy_fast(a,b,c) memcpy(a,b,c)
83 #else /* __COVERITY__ */
84 #define clib_memcpy_fast(a,b,c) memcpy(a,b,c)
87 /* c-11 string manipulation variants */
103 * In order to provide smooth mapping from unsafe string API to the clib string
104 * macro, we often have to improvise s1max and s2max due to the additional
105 * arguments are required for implementing the safe API. This macro is used
106 * to provide the s1max/s2max. It is not perfect because the actual
107 * s1max/s2max may be greater than 4k and the mapping from the unsafe API to
108 * the macro would cause a regression. However, it is not terribly likely.
109 * So I bet against the odds.
111 #define CLIB_STRING_MACRO_MAX 4096
114 typedef uword rsize_t;
116 void clib_c11_violation (const char *s);
117 errno_t memcpy_s (void *__restrict__ dest, rsize_t dmax,
118 const void *__restrict__ src, rsize_t n);
120 always_inline errno_t
121 memcpy_s_inline (void *__restrict__ dest, rsize_t dmax,
122 const void *__restrict__ src, rsize_t n)
128 * Optimize constant-number-of-bytes calls without asking
129 * "too many questions for someone from New Jersey"
131 if (__builtin_constant_p (n))
133 clib_memcpy_fast (dest, src, n);
138 * call bogus if: src or dst NULL, trying to copy
139 * more data than we have space in dst, or src == dst.
140 * n == 0 isn't really "bad", so check first in the
141 * "wall-of-shame" department...
143 bad = (dest == 0) + (src == 0) + (n > dmax) + (dest == src) + (n == 0);
144 if (PREDICT_FALSE (bad != 0))
146 /* Not actually trying to copy anything is OK */
150 clib_c11_violation ("dest NULL");
152 clib_c11_violation ("src NULL");
154 clib_c11_violation ("n > dmax");
156 clib_c11_violation ("dest == src");
160 /* Check for src/dst overlap, which is not allowed */
161 low = (uword) (src < dest ? src : dest);
162 hi = (uword) (src < dest ? dest : src);
164 if (PREDICT_FALSE (low + (n - 1) >= hi))
166 clib_c11_violation ("src/dest overlap");
170 clib_memcpy_fast (dest, src, n);
175 * Note: $$$ This macro is a crutch. Folks need to manually
176 * inspect every extant clib_memcpy(...) call and
177 * attempt to provide a real destination buffer size
180 #define clib_memcpy(d,s,n) memcpy_s_inline(d,n,s,n)
182 errno_t memset_s (void *s, rsize_t smax, int c, rsize_t n);
184 always_inline errno_t
185 memset_s_inline (void *s, rsize_t smax, int c, rsize_t n)
189 bad = (s == 0) + (n > smax);
191 if (PREDICT_FALSE (bad != 0))
194 clib_c11_violation ("s NULL");
196 clib_c11_violation ("n > smax");
204 * This macro is not [so much of] a crutch.
205 * It's super-typical to write:
207 * ep = pool_get (<pool>);
208 * clib_memset(ep, 0, sizeof (*ep));
210 * The compiler should delete the not-so useful
211 * (n > smax) test. TBH the NULL pointer check isn't
212 * so useful in this case, but so be it.
214 #define clib_memset(s,c,n) memset_s_inline(s,n,c,n)
216 static_always_inline void
217 clib_memset_u64 (void *p, u64 val, uword count)
220 #if defined(CLIB_HAVE_VEC512)
221 u64x8 v512 = u64x8_splat (val);
224 u64x8_store_unaligned (v512, ptr);
231 #if defined(CLIB_HAVE_VEC256)
232 u64x4 v256 = u64x4_splat (val);
235 u64x4_store_unaligned (v256, ptr);
244 ptr[0] = ptr[1] = ptr[2] = ptr[3] = val;
253 static_always_inline void
254 clib_memset_u32 (void *p, u32 val, uword count)
257 #if defined(CLIB_HAVE_VEC512)
258 u32x16 v512 = u32x16_splat (val);
261 u32x16_store_unaligned (v512, ptr);
268 #if defined(CLIB_HAVE_VEC256)
269 u32x8 v256 = u32x8_splat (val);
272 u32x8_store_unaligned (v256, ptr);
279 #if defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_UNALIGNED_LOAD_STORE)
280 u32x4 v128 = u32x4_splat (val);
283 u32x4_store_unaligned (v128, ptr);
290 ptr[0] = ptr[1] = ptr[2] = ptr[3] = val;
299 static_always_inline void
300 clib_memset_u16 (void *p, u16 val, uword count)
303 #if defined(CLIB_HAVE_VEC512)
304 u16x32 v512 = u16x32_splat (val);
307 u16x32_store_unaligned (v512, ptr);
314 #if defined(CLIB_HAVE_VEC256)
315 u16x16 v256 = u16x16_splat (val);
318 u16x16_store_unaligned (v256, ptr);
325 #if defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_UNALIGNED_LOAD_STORE)
326 u16x8 v128 = u16x8_splat (val);
329 u16x8_store_unaligned (v128, ptr);
336 ptr[0] = ptr[1] = ptr[2] = ptr[3] = val;
345 static_always_inline void
346 clib_memset_u8 (void *p, u8 val, uword count)
349 #if defined(CLIB_HAVE_VEC512)
350 u8x64 v512 = u8x64_splat (val);
353 u8x64_store_unaligned (v512, ptr);
360 #if defined(CLIB_HAVE_VEC256)
361 u8x32 v256 = u8x32_splat (val);
364 u8x32_store_unaligned (v256, ptr);
371 #if defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_UNALIGNED_LOAD_STORE)
372 u8x16 v128 = u8x16_splat (val);
375 u8x16_store_unaligned (v128, ptr);
382 ptr[0] = ptr[1] = ptr[2] = ptr[3] = val;
391 static_always_inline uword
392 clib_count_equal_u64 (u64 * data, uword max_count)
399 if (data[0] != data[1])
405 #if defined(CLIB_HAVE_VEC256)
406 u64x4 splat = u64x4_splat (first);
410 bmp = u8x32_msb_mask ((u8x32) (u64x4_load_unaligned (data) == splat));
411 if (bmp != 0xffffffff)
413 count += count_trailing_zeros (~bmp) / 8;
414 return clib_min (count, max_count);
420 if (count >= max_count)
426 while (count + 3 < max_count &&
427 ((data[0] ^ first) | (data[1] ^ first) |
428 (data[2] ^ first) | (data[3] ^ first)) == 0)
433 while (count < max_count && (data[0] == first))
441 static_always_inline uword
442 clib_count_equal_u32 (u32 * data, uword max_count)
449 if (data[0] != data[1])
455 #if defined(CLIB_HAVE_VEC256)
456 u32x8 splat = u32x8_splat (first);
460 bmp = u8x32_msb_mask ((u8x32) (u32x8_load_unaligned (data) == splat));
461 if (bmp != 0xffffffff)
463 count += count_trailing_zeros (~bmp) / 4;
464 return clib_min (count, max_count);
470 if (count >= max_count)
473 #elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK)
474 u32x4 splat = u32x4_splat (first);
478 bmp = u8x16_msb_mask ((u8x16) (u32x4_load_unaligned (data) == splat));
481 count += count_trailing_zeros (~bmp) / 4;
482 return clib_min (count, max_count);
488 if (count >= max_count)
494 while (count + 3 < max_count &&
495 ((data[0] ^ first) | (data[1] ^ first) |
496 (data[2] ^ first) | (data[3] ^ first)) == 0)
501 while (count < max_count && (data[0] == first))
509 static_always_inline uword
510 clib_count_equal_u16 (u16 * data, uword max_count)
517 if (data[0] != data[1])
523 #if defined(CLIB_HAVE_VEC256)
524 u16x16 splat = u16x16_splat (first);
528 bmp = u8x32_msb_mask ((u8x32) (u16x16_load_unaligned (data) == splat));
529 if (bmp != 0xffffffff)
531 count += count_trailing_zeros (~bmp) / 2;
532 return clib_min (count, max_count);
538 if (count >= max_count)
541 #elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK)
542 u16x8 splat = u16x8_splat (first);
546 bmp = u8x16_msb_mask ((u8x16) (u16x8_load_unaligned (data) == splat));
549 count += count_trailing_zeros (~bmp) / 2;
550 return clib_min (count, max_count);
556 if (count >= max_count)
562 while (count + 3 < max_count &&
563 ((data[0] ^ first) | (data[1] ^ first) |
564 (data[2] ^ first) | (data[3] ^ first)) == 0)
569 while (count < max_count && (data[0] == first))
577 static_always_inline uword
578 clib_count_equal_u8 (u8 * data, uword max_count)
585 if (data[0] != data[1])
591 #if defined(CLIB_HAVE_VEC256)
592 u8x32 splat = u8x32_splat (first);
596 bmp = u8x32_msb_mask ((u8x32) (u8x32_load_unaligned (data) == splat));
597 if (bmp != 0xffffffff)
599 count += count_trailing_zeros (~bmp);
600 return clib_min (count, max_count);
606 if (count >= max_count)
609 #elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK)
610 u8x16 splat = u8x16_splat (first);
614 bmp = u8x16_msb_mask ((u8x16) (u8x16_load_unaligned (data) == splat));
617 count += count_trailing_zeros (~bmp);
618 return clib_min (count, max_count);
624 if (count >= max_count)
630 while (count + 3 < max_count &&
631 ((data[0] ^ first) | (data[1] ^ first) |
632 (data[2] ^ first) | (data[3] ^ first)) == 0)
637 while (count < max_count && (data[0] == first))
646 * This macro is to provide smooth mapping from memcmp to memcmp_s.
647 * memcmp has fewer parameters and fewer returns than memcmp_s.
648 * This macro is somewhat a crutch. When err != EOK is returned from memcmp_s,
649 * we return 0 and spit out a message in the console because there is
650 * no way to return the error code to the memcmp callers.
651 * This condition happens when s1 or s2 is null. Please note
652 * in the extant memcmp calls, if s1, s2, or both are null, memcmp returns 0
653 * anyway. So we are consistent in this case for the comparison return
654 * although we also spit out a C11 violation message in the console to
655 * warn that they pass null pointers for both s1 and s2.
656 * Applications are encouraged to use the cool C11 memcmp_s API to get the
657 * maximum benefit out of it.
659 #define clib_memcmp(s1,s2,m1) \
661 memcmp_s_inline (s1, m1, s2, m1, &__diff); \
665 errno_t memcmp_s (const void *s1, rsize_t s1max, const void *s2,
666 rsize_t s2max, int *diff);
668 always_inline errno_t
669 memcmp_s_inline (const void *s1, rsize_t s1max, const void *s2, rsize_t s2max,
674 bad = (s1 == 0) + (s2 == 0) + (diff == 0) + (s2max > s1max) + (s2max == 0) +
677 if (PREDICT_FALSE (bad != 0))
680 clib_c11_violation ("s1 NULL");
682 clib_c11_violation ("s2 NULL");
684 clib_c11_violation ("diff NULL");
686 clib_c11_violation ("s2max > s1max");
688 clib_c11_violation ("s2max 0");
690 clib_c11_violation ("s1max 0");
694 if (PREDICT_FALSE (s1 == s2))
700 *diff = memcmp (s1, s2, s2max);
705 * This macro is to provide smooth mapping from strnlen to strnlen_s
707 #define clib_strnlen(s,m) strnlen_s_inline(s,m)
709 size_t strnlen_s (const char *s, size_t maxsize);
712 strnlen_s_inline (const char *s, size_t maxsize)
716 bad = (s == 0) + (maxsize == 0);
717 if (PREDICT_FALSE (bad != 0))
720 clib_c11_violation ("s NULL");
722 clib_c11_violation ("maxsize 0");
725 return strnlen (s, maxsize);
729 * This macro is to provide smooth mapping from strcmp to strcmp_s.
730 * strcmp has fewer parameters and fewer returns than strcmp_s.
731 * This macro is somewhat a crutch. When err != EOK is returned from strcmp_s,
732 * we return 0 and spit out a message in the console because
733 * there is no way to return the error to the strcmp callers.
734 * This condition happens when s1 or s2 is null. Please note in the extant
735 * strcmp call, they would end up crashing if one of them is null.
736 * So the new behavior is no crash, but an error is displayed in the
737 * console which I think is more user friendly. If both s1 and s2 are null,
738 * strcmp returns 0. Obviously, strcmp did the pointers comparison prior
739 * to actually accessing the pointer contents. We are still consistent
740 * in this case for the comparison return although we also spit out a
741 * C11 violation message in the console to warn that they pass null pointers
742 * for both s1 and s2. The other problem is strcmp does not provide s1max,
743 * we use CLIB_STRING_MACRO_MAX and hopefully, s1 is null terminated.
744 * If not, we may be accessing memory beyonf what is intended.
745 * Applications are encouraged to use the cool C11 strcmp_s API to get the
746 * maximum benefit out of it.
748 #define clib_strcmp(s1,s2) \
749 ({ int __indicator = 0; \
750 strcmp_s_inline (s1, CLIB_STRING_MACRO_MAX, s2, &__indicator); \
754 errno_t strcmp_s (const char *s1, rsize_t s1max, const char *s2,
757 always_inline errno_t
758 strcmp_s_inline (const char *s1, rsize_t s1max, const char *s2,
763 bad = (indicator == 0) + (s1 == 0) + (s2 == 0) + (s1max == 0) +
764 (s1 && s1max && s1[clib_strnlen (s1, s1max)] != '\0');
766 if (PREDICT_FALSE (bad != 0))
768 if (indicator == NULL)
769 clib_c11_violation ("indicator NULL");
771 clib_c11_violation ("s1 NULL");
773 clib_c11_violation ("s2 NULL");
775 clib_c11_violation ("s1max 0");
776 if (s1 && s1max && s1[clib_strnlen (s1, s1max)] != '\0')
777 clib_c11_violation ("s1 unterminated");
781 *indicator = strcmp (s1, s2);
786 * This macro is to provide smooth mapping from strncmp to strncmp_s.
787 * strncmp has fewer parameters and fewer returns than strncmp_s. That said,
788 * this macro is somewhat a crutch. When we get err != EOK from strncmp_s,
789 * we return 0 and spit out a message in the console because there is no
790 * means to return the error to the strncmp caller.
791 * This condition happens when s1 or s2 is null. In the extant strncmp call,
792 * they would end up crashing if one of them is null. So the new behavior is
793 * no crash, but error is displayed in the console which is more
794 * user friendly. If s1 and s2 are null, strncmp returns 0. Obviously,
795 * strncmp did the pointers comparison prior to actually accessing the
796 * pointer contents. We are still consistent in this case for the comparison
797 * return although we also spit out a C11 violation message in the console to
798 * warn that they pass null pointers for both s1 and s2.
799 * Applications are encouraged to use the cool C11 strncmp_s API to get the
800 * maximum benefit out of it.
802 #define clib_strncmp(s1,s2,n) \
803 ({ int __indicator = 0; \
804 strncmp_s_inline (s1, CLIB_STRING_MACRO_MAX, s2, n, &__indicator); \
808 errno_t strncmp_s (const char *s1, rsize_t s1max, const char *s2, rsize_t n,
811 always_inline errno_t
812 strncmp_s_inline (const char *s1, rsize_t s1max, const char *s2, rsize_t n,
816 u8 s1_greater_s1max = (s1 && s1max && n > clib_strnlen (s1, s1max));
818 if (PREDICT_FALSE (s1_greater_s1max && indicator))
821 * strcmp allows n > s1max. If indicator is non null, we can still
822 * do the compare without any harm and return EINVAL as well as the
823 * result in indicator.
825 clib_c11_violation ("n exceeds s1 length");
826 *indicator = strncmp (s1, s2, n);
830 bad = (s1 == 0) + (s2 == 0) + (indicator == 0) + (s1max == 0) +
831 (s1 && s1max && s1[clib_strnlen (s1, s1max)] != '\0') + s1_greater_s1max;
833 if (PREDICT_FALSE (bad != 0))
835 if (indicator == NULL)
836 clib_c11_violation ("indicator NULL");
838 clib_c11_violation ("s1 NULL");
840 clib_c11_violation ("s2 NULL");
842 clib_c11_violation ("s1max 0");
843 if (s1 && s1max && s1[clib_strnlen (s1, s1max)] != '\0')
844 clib_c11_violation ("s1 unterminated");
845 if (s1_greater_s1max)
846 clib_c11_violation ("n exceeds s1 length");
850 *indicator = strncmp (s1, s2, n);
855 * This macro is provided for smooth migration from strcpy. It is not perfect
856 * because we don't know the size of the destination buffer to pass to strcpy_s.
857 * We improvise dmax with CLIB_STRING_MACRO_MAX.
858 * Applications are encouraged to move to the C11 strcpy_s API.
860 #define clib_strcpy(d,s) strcpy_s_inline(d,CLIB_STRING_MACRO_MAX,s)
862 errno_t strcpy_s (char *__restrict__ dest, rsize_t dmax,
863 const char *__restrict__ src);
865 always_inline errno_t
866 strcpy_s_inline (char *__restrict__ dest, rsize_t dmax,
867 const char *__restrict__ src)
873 bad = (dest == 0) + (dmax == 0) + (src == 0);
874 if (PREDICT_FALSE (bad != 0))
877 clib_c11_violation ("dest NULL");
879 clib_c11_violation ("src NULL");
881 clib_c11_violation ("dmax 0");
885 n = clib_strnlen (src, dmax);
886 if (PREDICT_FALSE (n >= dmax))
888 clib_c11_violation ("not enough space for dest");
891 /* Not actually trying to copy anything is OK */
892 if (PREDICT_FALSE (n == 0))
895 /* Check for src/dst overlap, which is not allowed */
896 low = (uword) (src < dest ? src : dest);
897 hi = (uword) (src < dest ? dest : src);
899 if (PREDICT_FALSE (low + (n - 1) >= hi))
901 clib_c11_violation ("src/dest overlap");
905 clib_memcpy_fast (dest, src, n);
911 * This macro is provided for smooth migration from strncpy. It is not perfect
912 * because we don't know the size of the destination buffer to pass to
913 * strncpy_s. We improvise dmax with CLIB_STRING_MACRO_MAX.
914 * Applications are encouraged to move to the C11 strncpy_s API and provide
915 * the correct dmax for better error checking.
917 #define clib_strncpy(d,s,n) strncpy_s_inline(d,CLIB_STRING_MACRO_MAX,s,n)
920 strncpy_s (char *__restrict__ dest, rsize_t dmax,
921 const char *__restrict__ src, rsize_t n);
923 always_inline errno_t
924 strncpy_s_inline (char *__restrict__ dest, rsize_t dmax,
925 const char *__restrict__ src, rsize_t n)
930 errno_t status = EOK;
932 bad = (dest == 0) + (dmax == 0) + (src == 0) + (n == 0);
933 if (PREDICT_FALSE (bad != 0))
935 /* Not actually trying to copy anything is OK */
939 clib_c11_violation ("dest NULL");
941 clib_c11_violation ("src NULL");
943 clib_c11_violation ("dmax 0");
947 if (PREDICT_FALSE (n >= dmax))
949 /* Relax and use strnlen of src */
950 clib_c11_violation ("n >= dmax");
951 m = clib_strnlen (src, dmax);
954 /* Truncate, adjust copy length to fit dest */
960 /* cap the copy to strlen(src) in case n > strlen(src) */
961 m = clib_strnlen (src, n);
963 /* Check for src/dst overlap, which is not allowed */
964 low = (uword) (src < dest ? src : dest);
965 hi = (uword) (src < dest ? dest : src);
968 * This check may fail innocently if src + dmax >= dst, but
969 * src + strlen(src) < dst. If it fails, check more carefully before
970 * blowing the whistle.
972 if (PREDICT_FALSE (low + (m - 1) >= hi))
974 m = clib_strnlen (src, m);
976 if (low + (m - 1) >= hi)
978 clib_c11_violation ("src/dest overlap");
983 clib_memcpy_fast (dest, src, m);
989 * This macro is to provide smooth migration from strcat to strcat_s.
990 * Because there is no dmax in strcat, we improvise it with
991 * CLIB_STRING_MACRO_MAX. Please note there may be a chance to overwrite dest
992 * with too many bytes from src.
993 * Applications are encouraged to use C11 API to provide the actual dmax
994 * for proper checking and protection.
996 #define clib_strcat(d,s) strcat_s_inline(d,CLIB_STRING_MACRO_MAX,s)
998 errno_t strcat_s (char *__restrict__ dest, rsize_t dmax,
999 const char *__restrict__ src);
1001 always_inline errno_t
1002 strcat_s_inline (char *__restrict__ dest, rsize_t dmax,
1003 const char *__restrict__ src)
1007 size_t m, n, dest_size;
1009 bad = (dest == 0) + (dmax == 0) + (src == 0);
1010 if (PREDICT_FALSE (bad != 0))
1013 clib_c11_violation ("dest NULL");
1015 clib_c11_violation ("src NULL");
1017 clib_c11_violation ("dmax 0");
1021 dest_size = clib_strnlen (dest, dmax);
1022 m = dmax - dest_size;
1023 n = clib_strnlen (src, m);
1024 if (PREDICT_FALSE (n >= m))
1026 clib_c11_violation ("not enough space for dest");
1030 /* Not actually trying to concatenate anything is OK */
1031 if (PREDICT_FALSE (n == 0))
1034 /* Check for src/dst overlap, which is not allowed */
1035 low = (uword) (src < dest ? src : dest);
1036 hi = (uword) (src < dest ? dest : src);
1038 if (PREDICT_FALSE (low + (n - 1) >= hi))
1040 clib_c11_violation ("src/dest overlap");
1044 clib_memcpy_fast (dest + dest_size, src, n);
1045 dest[dest_size + n] = '\0';
1050 * This macro is to provide smooth migration from strncat to strncat_s.
1051 * The unsafe strncat does not have s1max. We improvise it with
1052 * CLIB_STRING_MACRO_MAX. Please note there may be a chance to overwrite
1053 * dest with too many bytes from src.
1054 * Applications are encouraged to move to C11 strncat_s which requires dmax
1055 * from the caller and provides checking to safeguard the memory corruption.
1057 #define clib_strncat(d,s,n) strncat_s_inline(d,CLIB_STRING_MACRO_MAX,s,n)
1059 errno_t strncat_s (char *__restrict__ dest, rsize_t dmax,
1060 const char *__restrict__ src, rsize_t n);
1062 always_inline errno_t
1063 strncat_s_inline (char *__restrict__ dest, rsize_t dmax,
1064 const char *__restrict__ src, rsize_t n)
1068 size_t m, dest_size, allowed_size;
1069 errno_t status = EOK;
1071 bad = (dest == 0) + (src == 0) + (dmax == 0) + (n == 0);
1072 if (PREDICT_FALSE (bad != 0))
1074 /* Not actually trying to concatenate anything is OK */
1078 clib_c11_violation ("dest NULL");
1080 clib_c11_violation ("src NULL");
1082 clib_c11_violation ("dmax 0");
1086 /* Check for src/dst overlap, which is not allowed */
1087 low = (uword) (src < dest ? src : dest);
1088 hi = (uword) (src < dest ? dest : src);
1090 if (PREDICT_FALSE (low + (n - 1) >= hi))
1092 clib_c11_violation ("src/dest overlap");
1096 dest_size = clib_strnlen (dest, dmax);
1097 allowed_size = dmax - dest_size;
1099 if (PREDICT_FALSE (allowed_size == 0))
1101 clib_c11_violation ("no space left in dest");
1105 if (PREDICT_FALSE (n >= allowed_size))
1108 * unlike strcat_s, strncat_s will do the concatenation anyway when
1109 * there is not enough space in dest. But it will do the truncation and
1110 * null terminate dest
1112 m = clib_strnlen (src, allowed_size);
1113 if (m >= allowed_size)
1115 m = allowed_size - 1;
1120 m = clib_strnlen (src, n);
1122 clib_memcpy_fast (dest + dest_size, src, m);
1123 dest[dest_size + m] = '\0';
1128 * This macro is to provide smooth mapping from strtok_r to strtok_s.
1129 * To map strtok to this macro, the caller would have to supply an additional
1130 * argument. strtokr_s requires s1max which the unsafe API does not have. So
1131 * we have to improvise it with CLIB_STRING_MACRO_MAX. Unlike strtok_s,
1132 * this macro cannot catch unterminated s1 and s2.
1133 * Applications are encouraged to use the cool C11 strtok_s API to avoid
1136 #define clib_strtok(s1,s2,p) \
1137 ({ rsize_t __s1max = CLIB_STRING_MACRO_MAX; \
1138 strtok_s_inline (s1, &__s1max, s2, p); \
1141 char *strtok_s (char *__restrict__ s1, rsize_t * __restrict__ s1max,
1142 const char *__restrict__ s2, char **__restrict__ ptr);
1144 always_inline char *
1145 strtok_s_inline (char *__restrict__ s1, rsize_t * __restrict__ s1max,
1146 const char *__restrict__ s2, char **__restrict__ ptr)
1148 #define STRTOK_DELIM_MAX_LEN 16
1154 bad = (s1max == 0) + (s2 == 0) + (ptr == 0) +
1155 ((s1 == 0) && ptr && (*ptr == 0));
1156 if (PREDICT_FALSE (bad != 0))
1159 clib_c11_violation ("s2 NULL");
1161 clib_c11_violation ("s1max is NULL");
1163 clib_c11_violation ("ptr is NULL");
1164 /* s1 == 0 and *ptr == null is no good */
1165 if ((s1 == 0) && ptr && (*ptr == 0))
1166 clib_c11_violation ("s1 and ptr contents are NULL");
1174 * scan s1 for a delimiter
1178 while (*s1 != '\0' && !ptoken)
1180 if (PREDICT_FALSE (dlen == 0))
1183 clib_c11_violation ("s1 unterminated");
1188 * must scan the entire delimiter list
1189 * ISO should have included a delimiter string limit!!
1191 slen = STRTOK_DELIM_MAX_LEN;
1195 if (PREDICT_FALSE (slen == 0))
1198 clib_c11_violation ("s2 unterminated");
1218 * if the beginning of a token was not found, then no
1219 * need to continue the scan.
1228 * Now we need to locate the end of the token
1235 clib_c11_violation ("s1 unterminated");
1239 slen = STRTOK_DELIM_MAX_LEN;
1246 clib_c11_violation ("s2 unterminated");
1253 * found a delimiter, set to null
1254 * and return context ptr to next char
1257 *ptr = (s1 + 1); /* return pointer for next scan */
1258 *s1max = dlen - 1; /* account for the nulled delimiter */
1264 * simply scanning through the delimiter string
1279 * This macro is to provide smooth mapping from strstr to strstr_s.
1280 * strstr_s requires s1max and s2max which the unsafe API does not have. So
1281 * we have to improvise them with CLIB_STRING_MACRO_MAX which may cause us
1282 * to access memory beyond it is intended if s1 or s2 is unterminated.
1283 * For the record, strstr crashes if s1 or s2 is unterminated. But this macro
1285 * Applications are encouraged to use the cool C11 strstr_s API to avoid
1288 #define clib_strstr(s1,s2) \
1289 ({ char * __substring = 0; \
1290 strstr_s_inline (s1, CLIB_STRING_MACRO_MAX, s2, CLIB_STRING_MACRO_MAX, \
1295 errno_t strstr_s (char *s1, rsize_t s1max, const char *s2, rsize_t s2max,
1298 always_inline errno_t
1299 strstr_s_inline (char *s1, rsize_t s1max, const char *s2, rsize_t s2max,
1303 size_t s1_size, s2_size;
1306 (s1 == 0) + (s2 == 0) + (substring == 0) + (s1max == 0) + (s2max == 0) +
1307 (s1 && s1max && (s1[clib_strnlen (s1, s1max)] != '\0')) +
1308 (s2 && s2max && (s2[clib_strnlen (s2, s2max)] != '\0'));
1309 if (PREDICT_FALSE (bad != 0))
1312 clib_c11_violation ("s1 NULL");
1314 clib_c11_violation ("s2 NULL");
1316 clib_c11_violation ("s1max 0");
1318 clib_c11_violation ("s2max 0");
1320 clib_c11_violation ("substring NULL");
1321 if (s1 && s1max && (s1[clib_strnlen (s1, s1max)] != '\0'))
1322 clib_c11_violation ("s1 unterminated");
1323 if (s2 && s2max && (s2[clib_strnlen (s2, s1max)] != '\0'))
1324 clib_c11_violation ("s2 unterminated");
1329 * s2 points to a string with zero length, or s2 equals s1, return s1
1331 if (PREDICT_FALSE (*s2 == '\0' || s1 == s2))
1338 * s2_size > s1_size, it won't find match.
1340 s1_size = clib_strnlen (s1, s1max);
1341 s2_size = clib_strnlen (s2, s2max);
1342 if (PREDICT_FALSE (s2_size > s1_size))
1345 *substring = strstr (s1, s2);
1346 if (*substring == 0)
1352 #endif /* included_clib_string_h */
1355 * fd.io coding-style-patch-verification: ON
1358 * eval: (c-set-style "gnu")