lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 1991-2015 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include <memcopy.h> |
| 21 | |
| 22 | |
| 23 | char * |
| 24 | __strncat_chk (s1, s2, n, s1len) |
| 25 | char *s1; |
| 26 | const char *s2; |
| 27 | size_t n; |
| 28 | size_t s1len; |
| 29 | { |
| 30 | char c; |
| 31 | char *s = s1; |
| 32 | |
| 33 | /* Find the end of S1. */ |
| 34 | do |
| 35 | { |
| 36 | if (__glibc_unlikely (s1len-- == 0)) |
| 37 | __chk_fail (); |
| 38 | c = *s1++; |
| 39 | } |
| 40 | while (c != '\0'); |
| 41 | |
| 42 | /* Make S1 point before next character, so we can increment |
| 43 | it while memory is read (wins on pipelined cpus). */ |
| 44 | ++s1len; |
| 45 | s1 -= 2; |
| 46 | |
| 47 | if (n >= 4) |
| 48 | { |
| 49 | size_t n4 = n >> 2; |
| 50 | do |
| 51 | { |
| 52 | if (__glibc_unlikely (s1len-- == 0)) |
| 53 | __chk_fail (); |
| 54 | c = *s2++; |
| 55 | *++s1 = c; |
| 56 | if (c == '\0') |
| 57 | return s; |
| 58 | if (__glibc_unlikely (s1len-- == 0)) |
| 59 | __chk_fail (); |
| 60 | c = *s2++; |
| 61 | *++s1 = c; |
| 62 | if (c == '\0') |
| 63 | return s; |
| 64 | if (__glibc_unlikely (s1len-- == 0)) |
| 65 | __chk_fail (); |
| 66 | c = *s2++; |
| 67 | *++s1 = c; |
| 68 | if (c == '\0') |
| 69 | return s; |
| 70 | if (__glibc_unlikely (s1len-- == 0)) |
| 71 | __chk_fail (); |
| 72 | c = *s2++; |
| 73 | *++s1 = c; |
| 74 | if (c == '\0') |
| 75 | return s; |
| 76 | } while (--n4 > 0); |
| 77 | n &= 3; |
| 78 | } |
| 79 | |
| 80 | while (n > 0) |
| 81 | { |
| 82 | if (__glibc_unlikely (s1len-- == 0)) |
| 83 | __chk_fail (); |
| 84 | c = *s2++; |
| 85 | *++s1 = c; |
| 86 | if (c == '\0') |
| 87 | return s; |
| 88 | n--; |
| 89 | } |
| 90 | |
| 91 | if (c != '\0') |
| 92 | { |
| 93 | if (__glibc_unlikely (s1len-- == 0)) |
| 94 | __chk_fail (); |
| 95 | *++s1 = '\0'; |
| 96 | } |
| 97 | |
| 98 | return s; |
| 99 | } |