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_memcpy_le (u8 * dst, u8 * src, u8 len, u8 max_len)
219 #if defined (CLIB_HAVE_VEC256)
220 u8x32 s0, s1, d0, d1;
221 u8x32 mask = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
222 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
224 u8x32 lv = u8x32_splat (len);
225 u8x32 add = u8x32_splat (32);
227 s0 = u8x32_load_unaligned (src);
228 s1 = u8x32_load_unaligned (src + 32);
229 d0 = u8x32_load_unaligned (dst);
230 d1 = u8x32_load_unaligned (dst + 32);
232 d0 = u8x32_blend (d0, s0, u8x32_is_greater (lv, mask));
233 u8x32_store_unaligned (d0, dst);
239 d1 = u8x32_blend (d1, s1, u8x32_is_greater (lv, mask));
240 u8x32_store_unaligned (d1, dst + 32);
242 #elif defined (CLIB_HAVE_VEC128)
243 u8x16 s0, s1, s2, s3, d0, d1, d2, d3;
244 u8x16 mask = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
245 u8x16 lv = u8x16_splat (len);
246 u8x16 add = u8x16_splat (16);
248 s0 = u8x16_load_unaligned (src);
249 s1 = u8x16_load_unaligned (src + 16);
250 s2 = u8x16_load_unaligned (src + 32);
251 s3 = u8x16_load_unaligned (src + 48);
252 d0 = u8x16_load_unaligned (dst);
253 d1 = u8x16_load_unaligned (dst + 16);
254 d2 = u8x16_load_unaligned (dst + 32);
255 d3 = u8x16_load_unaligned (dst + 48);
257 d0 = u8x16_blend (d0, s0, u8x16_is_greater (lv, mask));
258 u8x16_store_unaligned (d0, dst);
264 d1 = u8x16_blend (d1, s1, u8x16_is_greater (lv, mask));
265 u8x16_store_unaligned (d1, dst + 16);
271 d2 = u8x16_blend (d2, s2, u8x16_is_greater (lv, mask));
272 u8x16_store_unaligned (d2, dst + 32);
275 d3 = u8x16_blend (d3, s3, u8x16_is_greater (lv, mask));
276 u8x16_store_unaligned (d3, dst + 48);
278 memmove (dst, src, len);
282 static_always_inline void
283 clib_memcpy_le64 (u8 * dst, u8 * src, u8 len)
285 clib_memcpy_le (dst, src, len, 64);
288 static_always_inline void
289 clib_memcpy_le32 (u8 * dst, u8 * src, u8 len)
291 clib_memcpy_le (dst, src, len, 32);
294 static_always_inline void
295 clib_memset_u64 (void *p, u64 val, uword count)
298 #if defined(CLIB_HAVE_VEC512)
299 u64x8 v512 = u64x8_splat (val);
302 u64x8_store_unaligned (v512, ptr);
309 #if defined(CLIB_HAVE_VEC256)
310 u64x4 v256 = u64x4_splat (val);
313 u64x4_store_unaligned (v256, ptr);
322 ptr[0] = ptr[1] = ptr[2] = ptr[3] = val;
331 static_always_inline void
332 clib_memset_u32 (void *p, u32 val, uword count)
335 #if defined(CLIB_HAVE_VEC512)
336 u32x16 v512 = u32x16_splat (val);
339 u32x16_store_unaligned (v512, ptr);
346 #if defined(CLIB_HAVE_VEC256)
347 u32x8 v256 = u32x8_splat (val);
350 u32x8_store_unaligned (v256, ptr);
357 #if defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_UNALIGNED_LOAD_STORE)
358 u32x4 v128 = u32x4_splat (val);
361 u32x4_store_unaligned (v128, ptr);
368 ptr[0] = ptr[1] = ptr[2] = ptr[3] = val;
377 static_always_inline void
378 clib_memset_u16 (void *p, u16 val, uword count)
381 #if defined(CLIB_HAVE_VEC512)
382 u16x32 v512 = u16x32_splat (val);
385 u16x32_store_unaligned (v512, ptr);
392 #if defined(CLIB_HAVE_VEC256)
393 u16x16 v256 = u16x16_splat (val);
396 u16x16_store_unaligned (v256, ptr);
403 #if defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_UNALIGNED_LOAD_STORE)
404 u16x8 v128 = u16x8_splat (val);
407 u16x8_store_unaligned (v128, ptr);
414 ptr[0] = ptr[1] = ptr[2] = ptr[3] = val;
423 static_always_inline void
424 clib_memset_u8 (void *p, u8 val, uword count)
427 #if defined(CLIB_HAVE_VEC512)
428 u8x64 v512 = u8x64_splat (val);
431 u8x64_store_unaligned (v512, ptr);
438 #if defined(CLIB_HAVE_VEC256)
439 u8x32 v256 = u8x32_splat (val);
442 u8x32_store_unaligned (v256, ptr);
449 #if defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_UNALIGNED_LOAD_STORE)
450 u8x16 v128 = u8x16_splat (val);
453 u8x16_store_unaligned (v128, ptr);
460 ptr[0] = ptr[1] = ptr[2] = ptr[3] = val;
469 static_always_inline uword
470 clib_count_equal_u64 (u64 * data, uword max_count)
477 if (data[0] != data[1])
483 #if defined(CLIB_HAVE_VEC256)
484 u64x4 splat = u64x4_splat (first);
485 while (count + 3 < max_count)
488 bmp = u8x32_msb_mask ((u8x32) (u64x4_load_unaligned (data) == splat));
489 if (bmp != 0xffffffff)
491 count += count_trailing_zeros (~bmp) / 8;
501 while (count + 3 < max_count &&
502 ((data[0] ^ first) | (data[1] ^ first) |
503 (data[2] ^ first) | (data[3] ^ first)) == 0)
509 while (count < max_count && (data[0] == first))
517 static_always_inline uword
518 clib_count_equal_u32 (u32 * data, uword max_count)
525 if (data[0] != data[1])
531 #if defined(CLIB_HAVE_VEC256)
532 u32x8 splat = u32x8_splat (first);
533 while (count + 7 < max_count)
536 bmp = u8x32_msb_mask ((u8x32) (u32x8_load_unaligned (data) == splat));
537 if (bmp != 0xffffffff)
539 count += count_trailing_zeros (~bmp) / 4;
546 #elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK)
547 u32x4 splat = u32x4_splat (first);
548 while (count + 3 < max_count)
551 bmp = u8x16_msb_mask ((u8x16) (u32x4_load_unaligned (data) == splat));
554 count += count_trailing_zeros (~bmp) / 4;
564 while (count + 3 < max_count &&
565 ((data[0] ^ first) | (data[1] ^ first) |
566 (data[2] ^ first) | (data[3] ^ first)) == 0)
572 while (count < max_count && (data[0] == first))
580 static_always_inline uword
581 clib_count_equal_u16 (u16 * data, uword max_count)
588 if (data[0] != data[1])
594 #if defined(CLIB_HAVE_VEC256)
595 u16x16 splat = u16x16_splat (first);
596 while (count + 15 < max_count)
599 bmp = u8x32_msb_mask ((u8x32) (u16x16_load_unaligned (data) == splat));
600 if (bmp != 0xffffffff)
602 count += count_trailing_zeros (~bmp) / 2;
609 #elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK)
610 u16x8 splat = u16x8_splat (first);
611 while (count + 7 < max_count)
614 bmp = u8x16_msb_mask ((u8x16) (u16x8_load_unaligned (data) == splat));
617 count += count_trailing_zeros (~bmp) / 2;
627 while (count + 3 < max_count &&
628 ((data[0] ^ first) | (data[1] ^ first) |
629 (data[2] ^ first) | (data[3] ^ first)) == 0)
635 while (count < max_count && (data[0] == first))
643 static_always_inline uword
644 clib_count_equal_u8 (u8 * data, uword max_count)
651 if (data[0] != data[1])
657 #if defined(CLIB_HAVE_VEC256)
658 u8x32 splat = u8x32_splat (first);
659 while (count + 31 < max_count)
662 bmp = u8x32_msb_mask ((u8x32) (u8x32_load_unaligned (data) == splat));
663 if (bmp != 0xffffffff)
665 count += count_trailing_zeros (~bmp);
672 #elif defined(CLIB_HAVE_VEC128) && defined(CLIB_HAVE_VEC128_MSB_MASK)
673 u8x16 splat = u8x16_splat (first);
674 while (count + 15 < max_count)
677 bmp = u8x16_msb_mask ((u8x16) (u8x16_load_unaligned (data) == splat));
680 count += count_trailing_zeros (~bmp);
690 while (count + 3 < max_count &&
691 ((data[0] ^ first) | (data[1] ^ first) |
692 (data[2] ^ first) | (data[3] ^ first)) == 0)
698 while (count < max_count && (data[0] == first))
707 * This macro is to provide smooth mapping from memcmp to memcmp_s.
708 * memcmp has fewer parameters and fewer returns than memcmp_s.
709 * This macro is somewhat a crutch. When err != EOK is returned from memcmp_s,
710 * we return 0 and spit out a message in the console because there is
711 * no way to return the error code to the memcmp callers.
712 * This condition happens when s1 or s2 is null. Please note
713 * in the extant memcmp calls, if s1, s2, or both are null, memcmp returns 0
714 * anyway. So we are consistent in this case for the comparison return
715 * although we also spit out a C11 violation message in the console to
716 * warn that they pass null pointers for both s1 and s2.
717 * Applications are encouraged to use the cool C11 memcmp_s API to get the
718 * maximum benefit out of it.
720 #define clib_memcmp(s1,s2,m1) \
722 memcmp_s_inline (s1, m1, s2, m1, &__diff); \
726 errno_t memcmp_s (const void *s1, rsize_t s1max, const void *s2,
727 rsize_t s2max, int *diff);
729 always_inline errno_t
730 memcmp_s_inline (const void *s1, rsize_t s1max, const void *s2, rsize_t s2max,
735 bad = (s1 == 0) + (s2 == 0) + (diff == 0) + (s2max > s1max) + (s2max == 0) +
738 if (PREDICT_FALSE (bad != 0))
741 clib_c11_violation ("s1 NULL");
743 clib_c11_violation ("s2 NULL");
745 clib_c11_violation ("diff NULL");
747 clib_c11_violation ("s2max > s1max");
749 clib_c11_violation ("s2max 0");
751 clib_c11_violation ("s1max 0");
755 if (PREDICT_FALSE (s1 == s2))
761 *diff = memcmp (s1, s2, s2max);
766 * This macro is to provide smooth mapping from strnlen to strnlen_s
768 #define clib_strnlen(s,m) strnlen_s_inline(s,m)
770 size_t strnlen_s (const char *s, size_t maxsize);
773 strnlen_s_inline (const char *s, size_t maxsize)
777 bad = (s == 0) + (maxsize == 0);
778 if (PREDICT_FALSE (bad != 0))
781 clib_c11_violation ("s NULL");
783 clib_c11_violation ("maxsize 0");
786 return strnlen (s, maxsize);
790 * This macro is to provide smooth mapping from strcmp to strcmp_s.
791 * strcmp has fewer parameters and fewer returns than strcmp_s.
792 * This macro is somewhat a crutch. When err != EOK is returned from strcmp_s,
793 * we return 0 and spit out a message in the console because
794 * there is no way to return the error to the strcmp callers.
795 * This condition happens when s1 or s2 is null. Please note in the extant
796 * strcmp call, they would end up crashing if one of them is null.
797 * So the new behavior is no crash, but an error is displayed in the
798 * console which I think is more user friendly. If both s1 and s2 are null,
799 * strcmp returns 0. Obviously, strcmp did the pointers comparison prior
800 * to actually accessing the pointer contents. We are still consistent
801 * in this case for the comparison return although we also spit out a
802 * C11 violation message in the console to warn that they pass null pointers
803 * for both s1 and s2. The other problem is strcmp does not provide s1max,
804 * we use CLIB_STRING_MACRO_MAX and hopefully, s1 is null terminated.
805 * If not, we may be accessing memory beyonf what is intended.
806 * Applications are encouraged to use the cool C11 strcmp_s API to get the
807 * maximum benefit out of it.
809 #define clib_strcmp(s1,s2) \
810 ({ int __indicator = 0; \
811 strcmp_s_inline (s1, CLIB_STRING_MACRO_MAX, s2, &__indicator); \
815 errno_t strcmp_s (const char *s1, rsize_t s1max, const char *s2,
818 always_inline errno_t
819 strcmp_s_inline (const char *s1, rsize_t s1max, const char *s2,
824 bad = (indicator == 0) + (s1 == 0) + (s2 == 0) + (s1max == 0) +
825 (s1 && s1max && s1[clib_strnlen (s1, s1max)] != '\0');
827 if (PREDICT_FALSE (bad != 0))
829 if (indicator == NULL)
830 clib_c11_violation ("indicator NULL");
832 clib_c11_violation ("s1 NULL");
834 clib_c11_violation ("s2 NULL");
836 clib_c11_violation ("s1max 0");
837 if (s1 && s1max && s1[clib_strnlen (s1, s1max)] != '\0')
838 clib_c11_violation ("s1 unterminated");
842 *indicator = strcmp (s1, s2);
847 * This macro is to provide smooth mapping from strncmp to strncmp_s.
848 * strncmp has fewer parameters and fewer returns than strncmp_s. That said,
849 * this macro is somewhat a crutch. When we get err != EOK from strncmp_s,
850 * we return 0 and spit out a message in the console because there is no
851 * means to return the error to the strncmp caller.
852 * This condition happens when s1 or s2 is null. In the extant strncmp call,
853 * they would end up crashing if one of them is null. So the new behavior is
854 * no crash, but error is displayed in the console which is more
855 * user friendly. If s1 and s2 are null, strncmp returns 0. Obviously,
856 * strncmp did the pointers comparison prior to actually accessing the
857 * pointer contents. We are still consistent in this case for the comparison
858 * return although we also spit out a C11 violation message in the console to
859 * warn that they pass null pointers for both s1 and s2.
860 * Applications are encouraged to use the cool C11 strncmp_s API to get the
861 * maximum benefit out of it.
863 #define clib_strncmp(s1,s2,n) \
864 ({ int __indicator = 0; \
865 strncmp_s_inline (s1, CLIB_STRING_MACRO_MAX, s2, n, &__indicator); \
869 errno_t strncmp_s (const char *s1, rsize_t s1max, const char *s2, rsize_t n,
872 always_inline errno_t
873 strncmp_s_inline (const char *s1, rsize_t s1max, const char *s2, rsize_t n,
877 u8 s1_greater_s1max = (s1 && s1max && n > clib_strnlen (s1, s1max));
879 if (PREDICT_FALSE (s1_greater_s1max && indicator))
882 * strcmp allows n > s1max. If indicator is non null, we can still
883 * do the compare without any harm and return EINVAL as well as the
884 * result in indicator.
886 clib_c11_violation ("n exceeds s1 length");
887 *indicator = strncmp (s1, s2, n);
891 bad = (s1 == 0) + (s2 == 0) + (indicator == 0) + (s1max == 0) +
892 (s1 && s1max && s1[clib_strnlen (s1, s1max)] != '\0') + s1_greater_s1max;
894 if (PREDICT_FALSE (bad != 0))
896 if (indicator == NULL)
897 clib_c11_violation ("indicator NULL");
899 clib_c11_violation ("s1 NULL");
901 clib_c11_violation ("s2 NULL");
903 clib_c11_violation ("s1max 0");
904 if (s1 && s1max && s1[clib_strnlen (s1, s1max)] != '\0')
905 clib_c11_violation ("s1 unterminated");
906 if (s1_greater_s1max)
907 clib_c11_violation ("n exceeds s1 length");
911 *indicator = strncmp (s1, s2, n);
916 * This macro is provided for smooth migration from strcpy. It is not perfect
917 * because we don't know the size of the destination buffer to pass to strcpy_s.
918 * We improvise dmax with CLIB_STRING_MACRO_MAX.
919 * Applications are encouraged to move to the C11 strcpy_s API.
921 #define clib_strcpy(d,s) strcpy_s_inline(d,CLIB_STRING_MACRO_MAX,s)
923 errno_t strcpy_s (char *__restrict__ dest, rsize_t dmax,
924 const char *__restrict__ src);
926 always_inline errno_t
927 strcpy_s_inline (char *__restrict__ dest, rsize_t dmax,
928 const char *__restrict__ src)
934 bad = (dest == 0) + (dmax == 0) + (src == 0);
935 if (PREDICT_FALSE (bad != 0))
938 clib_c11_violation ("dest NULL");
940 clib_c11_violation ("src NULL");
942 clib_c11_violation ("dmax 0");
946 n = clib_strnlen (src, dmax);
947 if (PREDICT_FALSE (n >= dmax))
949 clib_c11_violation ("not enough space for dest");
952 /* Not actually trying to copy anything is OK */
953 if (PREDICT_FALSE (n == 0))
956 /* Check for src/dst overlap, which is not allowed */
957 low = (uword) (src < dest ? src : dest);
958 hi = (uword) (src < dest ? dest : src);
960 if (PREDICT_FALSE (low + (n - 1) >= hi))
962 clib_c11_violation ("src/dest overlap");
966 clib_memcpy_fast (dest, src, n);
972 * This macro is provided for smooth migration from strncpy. It is not perfect
973 * because we don't know the size of the destination buffer to pass to
974 * strncpy_s. We improvise dmax with CLIB_STRING_MACRO_MAX.
975 * Applications are encouraged to move to the C11 strncpy_s API and provide
976 * the correct dmax for better error checking.
978 #define clib_strncpy(d,s,n) strncpy_s_inline(d,CLIB_STRING_MACRO_MAX,s,n)
981 strncpy_s (char *__restrict__ dest, rsize_t dmax,
982 const char *__restrict__ src, rsize_t n);
984 always_inline errno_t
985 strncpy_s_inline (char *__restrict__ dest, rsize_t dmax,
986 const char *__restrict__ src, rsize_t n)
991 errno_t status = EOK;
993 bad = (dest == 0) + (dmax == 0) + (src == 0) + (n == 0);
994 if (PREDICT_FALSE (bad != 0))
996 /* Not actually trying to copy anything is OK */
1000 clib_c11_violation ("dest NULL");
1002 clib_c11_violation ("src NULL");
1004 clib_c11_violation ("dmax 0");
1008 if (PREDICT_FALSE (n >= dmax))
1010 /* Relax and use strnlen of src */
1011 clib_c11_violation ("n >= dmax");
1012 m = clib_strnlen (src, dmax);
1015 /* Truncate, adjust copy length to fit dest */
1021 /* cap the copy to strlen(src) in case n > strlen(src) */
1022 m = clib_strnlen (src, n);
1024 /* Check for src/dst overlap, which is not allowed */
1025 low = (uword) (src < dest ? src : dest);
1026 hi = (uword) (src < dest ? dest : src);
1029 * This check may fail innocently if src + dmax >= dst, but
1030 * src + strlen(src) < dst. If it fails, check more carefully before
1031 * blowing the whistle.
1033 if (PREDICT_FALSE (low + (m - 1) >= hi))
1035 m = clib_strnlen (src, m);
1037 if (low + (m - 1) >= hi)
1039 clib_c11_violation ("src/dest overlap");
1044 clib_memcpy_fast (dest, src, m);
1050 * This macro is to provide smooth migration from strcat to strcat_s.
1051 * Because there is no dmax in strcat, we improvise it with
1052 * CLIB_STRING_MACRO_MAX. Please note there may be a chance to overwrite dest
1053 * with too many bytes from src.
1054 * Applications are encouraged to use C11 API to provide the actual dmax
1055 * for proper checking and protection.
1057 #define clib_strcat(d,s) strcat_s_inline(d,CLIB_STRING_MACRO_MAX,s)
1059 errno_t strcat_s (char *__restrict__ dest, rsize_t dmax,
1060 const char *__restrict__ src);
1062 always_inline errno_t
1063 strcat_s_inline (char *__restrict__ dest, rsize_t dmax,
1064 const char *__restrict__ src)
1068 size_t m, n, dest_size;
1070 bad = (dest == 0) + (dmax == 0) + (src == 0);
1071 if (PREDICT_FALSE (bad != 0))
1074 clib_c11_violation ("dest NULL");
1076 clib_c11_violation ("src NULL");
1078 clib_c11_violation ("dmax 0");
1082 dest_size = clib_strnlen (dest, dmax);
1083 m = dmax - dest_size;
1084 n = clib_strnlen (src, m);
1085 if (PREDICT_FALSE (n >= m))
1087 clib_c11_violation ("not enough space for dest");
1091 /* Not actually trying to concatenate anything is OK */
1092 if (PREDICT_FALSE (n == 0))
1095 /* Check for src/dst overlap, which is not allowed */
1096 low = (uword) (src < dest ? src : dest);
1097 hi = (uword) (src < dest ? dest : src);
1099 if (PREDICT_FALSE (low + (n - 1) >= hi))
1101 clib_c11_violation ("src/dest overlap");
1105 clib_memcpy_fast (dest + dest_size, src, n);
1106 dest[dest_size + n] = '\0';
1111 * This macro is to provide smooth migration from strncat to strncat_s.
1112 * The unsafe strncat does not have s1max. We improvise it with
1113 * CLIB_STRING_MACRO_MAX. Please note there may be a chance to overwrite
1114 * dest with too many bytes from src.
1115 * Applications are encouraged to move to C11 strncat_s which requires dmax
1116 * from the caller and provides checking to safeguard the memory corruption.
1118 #define clib_strncat(d,s,n) strncat_s_inline(d,CLIB_STRING_MACRO_MAX,s,n)
1120 errno_t strncat_s (char *__restrict__ dest, rsize_t dmax,
1121 const char *__restrict__ src, rsize_t n);
1123 always_inline errno_t
1124 strncat_s_inline (char *__restrict__ dest, rsize_t dmax,
1125 const char *__restrict__ src, rsize_t n)
1129 size_t m, dest_size, allowed_size;
1130 errno_t status = EOK;
1132 bad = (dest == 0) + (src == 0) + (dmax == 0) + (n == 0);
1133 if (PREDICT_FALSE (bad != 0))
1135 /* Not actually trying to concatenate anything is OK */
1139 clib_c11_violation ("dest NULL");
1141 clib_c11_violation ("src NULL");
1143 clib_c11_violation ("dmax 0");
1147 /* Check for src/dst overlap, which is not allowed */
1148 low = (uword) (src < dest ? src : dest);
1149 hi = (uword) (src < dest ? dest : src);
1151 if (PREDICT_FALSE (low + (n - 1) >= hi))
1153 clib_c11_violation ("src/dest overlap");
1157 dest_size = clib_strnlen (dest, dmax);
1158 allowed_size = dmax - dest_size;
1160 if (PREDICT_FALSE (allowed_size == 0))
1162 clib_c11_violation ("no space left in dest");
1166 if (PREDICT_FALSE (n >= allowed_size))
1169 * unlike strcat_s, strncat_s will do the concatenation anyway when
1170 * there is not enough space in dest. But it will do the truncation and
1171 * null terminate dest
1173 m = clib_strnlen (src, allowed_size);
1174 if (m >= allowed_size)
1176 m = allowed_size - 1;
1181 m = clib_strnlen (src, n);
1183 clib_memcpy_fast (dest + dest_size, src, m);
1184 dest[dest_size + m] = '\0';
1189 * This macro is to provide smooth mapping from strtok_r to strtok_s.
1190 * To map strtok to this macro, the caller would have to supply an additional
1191 * argument. strtokr_s requires s1max which the unsafe API does not have. So
1192 * we have to improvise it with CLIB_STRING_MACRO_MAX. Unlike strtok_s,
1193 * this macro cannot catch unterminated s1 and s2.
1194 * Applications are encouraged to use the cool C11 strtok_s API to avoid
1197 #define clib_strtok(s1,s2,p) \
1198 ({ rsize_t __s1max = CLIB_STRING_MACRO_MAX; \
1199 strtok_s_inline (s1, &__s1max, s2, p); \
1202 char *strtok_s (char *__restrict__ s1, rsize_t * __restrict__ s1max,
1203 const char *__restrict__ s2, char **__restrict__ ptr);
1205 always_inline char *
1206 strtok_s_inline (char *__restrict__ s1, rsize_t * __restrict__ s1max,
1207 const char *__restrict__ s2, char **__restrict__ ptr)
1209 #define STRTOK_DELIM_MAX_LEN 16
1215 bad = (s1max == 0) + (s2 == 0) + (ptr == 0) +
1216 ((s1 == 0) && ptr && (*ptr == 0));
1217 if (PREDICT_FALSE (bad != 0))
1220 clib_c11_violation ("s2 NULL");
1222 clib_c11_violation ("s1max is NULL");
1224 clib_c11_violation ("ptr is NULL");
1225 /* s1 == 0 and *ptr == null is no good */
1226 if ((s1 == 0) && ptr && (*ptr == 0))
1227 clib_c11_violation ("s1 and ptr contents are NULL");
1235 * scan s1 for a delimiter
1239 while (*s1 != '\0' && !ptoken)
1241 if (PREDICT_FALSE (dlen == 0))
1244 clib_c11_violation ("s1 unterminated");
1249 * must scan the entire delimiter list
1250 * ISO should have included a delimiter string limit!!
1252 slen = STRTOK_DELIM_MAX_LEN;
1256 if (PREDICT_FALSE (slen == 0))
1259 clib_c11_violation ("s2 unterminated");
1279 * if the beginning of a token was not found, then no
1280 * need to continue the scan.
1289 * Now we need to locate the end of the token
1296 clib_c11_violation ("s1 unterminated");
1300 slen = STRTOK_DELIM_MAX_LEN;
1307 clib_c11_violation ("s2 unterminated");
1314 * found a delimiter, set to null
1315 * and return context ptr to next char
1318 *ptr = (s1 + 1); /* return pointer for next scan */
1319 *s1max = dlen - 1; /* account for the nulled delimiter */
1325 * simply scanning through the delimiter string
1340 * This macro is to provide smooth mapping from strstr to strstr_s.
1341 * strstr_s requires s1max and s2max which the unsafe API does not have. So
1342 * we have to improvise them with CLIB_STRING_MACRO_MAX which may cause us
1343 * to access memory beyond it is intended if s1 or s2 is unterminated.
1344 * For the record, strstr crashes if s1 or s2 is unterminated. But this macro
1346 * Applications are encouraged to use the cool C11 strstr_s API to avoid
1349 #define clib_strstr(s1,s2) \
1350 ({ char * __substring = 0; \
1351 strstr_s_inline (s1, CLIB_STRING_MACRO_MAX, s2, CLIB_STRING_MACRO_MAX, \
1356 errno_t strstr_s (char *s1, rsize_t s1max, const char *s2, rsize_t s2max,
1359 always_inline errno_t
1360 strstr_s_inline (char *s1, rsize_t s1max, const char *s2, rsize_t s2max,
1364 size_t s1_size, s2_size;
1367 (s1 == 0) + (s2 == 0) + (substring == 0) + (s1max == 0) + (s2max == 0) +
1368 (s1 && s1max && (s1[clib_strnlen (s1, s1max)] != '\0')) +
1369 (s2 && s2max && (s2[clib_strnlen (s2, s2max)] != '\0'));
1370 if (PREDICT_FALSE (bad != 0))
1373 clib_c11_violation ("s1 NULL");
1375 clib_c11_violation ("s2 NULL");
1377 clib_c11_violation ("s1max 0");
1379 clib_c11_violation ("s2max 0");
1381 clib_c11_violation ("substring NULL");
1382 if (s1 && s1max && (s1[clib_strnlen (s1, s1max)] != '\0'))
1383 clib_c11_violation ("s1 unterminated");
1384 if (s2 && s2max && (s2[clib_strnlen (s2, s1max)] != '\0'))
1385 clib_c11_violation ("s2 unterminated");
1390 * s2 points to a string with zero length, or s2 equals s1, return s1
1392 if (PREDICT_FALSE (*s2 == '\0' || s1 == s2))
1399 * s2_size > s1_size, it won't find match.
1401 s1_size = clib_strnlen (s1, s1max);
1402 s2_size = clib_strnlen (s2, s2max);
1403 if (PREDICT_FALSE (s2_size > s1_size))
1406 *substring = strstr (s1, s2);
1407 if (*substring == 0)
1413 #endif /* included_clib_string_h */
1416 * fd.io coding-style-patch-verification: ON
1419 * eval: (c-set-style "gnu")