1 /* lt__strl.c -- size-bounded string copying and concatenation 2 3 Copyright (C) 2004, 2011-2019, 2021-2022 Free Software Foundation, 4 Inc. 5 Written by Bob Friesenhahn, 2004 6 7 NOTE: The canonical source of this file is maintained with the 8 GNU Libtool package. Report bugs to bug-libtool@gnu.org. 9 10 GNU Libltdl is free software; you can redistribute it and/or 11 modify it under the terms of the GNU Lesser General Public 12 License as published by the Free Software Foundation; either 13 version 2 of the License, or (at your option) any later version. 14 15 As a special exception to the GNU Lesser General Public License, 16 if you distribute this file as part of a program or library that 17 is built using GNU Libtool, you may include this file under the 18 same distribution terms that you use for the rest of that program. 19 20 GNU Libltdl is distributed in the hope that it will be useful, 21 but WITHOUT ANY WARRANTY; without even the implied warranty of 22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 GNU Lesser General Public License for more details. 24 25 You should have received a copy of the GNU Lesser General Public 26 License along with GNU Libltdl; see the file COPYING.LIB. If not, a 27 copy can be downloaded from http://www.gnu.org/licenses/lgpl.html, 28 or obtained by writing to the Free Software Foundation, Inc., 29 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 30 */ 31 32 #include <assert.h> 33 #include <string.h> 34 35 #include "lt__strl.h" 36 37 /* 38 lt_strlcat appends the NULL-terminated string src to the end of dst. 39 It will append at most dstsize - strlen(dst) - 1 bytes, 40 NULL-terminating the result. The total length of the string that 41 would have been created given sufficient buffer size (may be longer 42 than dstsize) is returned. This function substitutes for strlcat(), 43 which is available under NetBSD, FreeBSD and Solaris 9. 44 45 Buffer overflow can be checked as follows: 46 47 if (lt_strlcat(dst, src, dstsize) >= dstsize) 48 return -1; 49 */ 50 #if !defined HAVE_STRLCAT 51 size_t 52 lt_strlcat(char *dst, const char *src, const size_t dstsize) /**/ 53 { 54 size_t length; 55 char *p; 56 const char *q; 57 58 assert(dst != NULL); 59 assert(src != (const char *) NULL); 60 assert(dstsize >= 1); 61 62 length=strlen(dst); 63 64 /* 65 Copy remaining characters from src while constraining length to 66 size - 1. 67 */ 68 for ( p = dst + length, q = src; 69 (*q != 0) && (length < dstsize - 1); 70 length++, p++, q++ ) 71 *p = *q; 72 73 dst[length]='\0'; 74 75 /* 76 Add remaining length of src to length. 77 */ 78 while (*q++) 79 length++; 80 81 return length; 82 } 83 #endif /* !defined HAVE_STRLCAT */ 84 85 /* 86 lt_strlcpy copies up to dstsize - 1 characters from the NULL-terminated 87 string src to dst, NULL-terminating the result. The total length of 88 the string that would have been created given sufficient buffer 89 size (may be longer than dstsize) is returned. This function 90 substitutes for strlcpy(), which is available under OpenBSD, FreeBSD 91 and Solaris 9. 92 93 Buffer overflow can be checked as follows: 94 95 if (lt_strlcpy(dst, src, dstsize) >= dstsize) 96 return -1; 97 */ 98 #if !defined HAVE_STRLCPY 99 size_t 100 lt_strlcpy(char *dst, const char *src, const size_t dstsize) /*
*/ 101 { 102 size_t length=0; 103 char *p; 104 const char *q; 105 106 assert(dst != NULL); 107 assert(src != (const char *) NULL); 108 assert(dstsize >= 1); 109 110 /* 111 Copy src to dst within bounds of size-1. 112 */ 113 for ( p=dst, q=src, length=0; 114 (*q != 0) && (length < dstsize-1); 115 length++, p++, q++ ) 116 *p = *q; 117 118 dst[length]='\0'; 119 120 /* 121 Add remaining length of src to length. 122 */ 123 while (*q++) 124 length++; 125 126 return length; 127 } 128 #endif /* !defined HAVE_STRLCPY */