Add support for strlcpy()

Too many places in xrdp use strncpy() to copy strings to fixed-length
buffers, when this is not the correct function to use.

This PR makes sure strlcpy() from the BSDs is available as a saner
alternative. This function is available by default on Linux and FreeBSD.
This commit is contained in:
matt335672
2025-03-25 10:37:45 +00:00
parent aac4946558
commit 960ea7ce05
3 changed files with 59 additions and 1 deletions
+53
View File
@@ -15,6 +15,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* The strlcpy implementation is taken from OpenBSD and reformatted. The
* original has the following notice attached:-
* |
* | Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
* |
* | Permission to use, copy, modify, and distribute this software for any
* | purpose with or without fee is hereby granted, provided that the above
* | copyright notice and this permission notice appear in all copies.
* |
* | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.*
*
* generic string handling calls
*/
@@ -448,6 +465,42 @@ g_atoix(const char *str)
return strtol(str, NULL, base);
}
/*****************************************************************************/
#if !defined(HAVE_STRLCPY)
size_t strlcpy(char *dst, const char *src, size_t dsize)
{
const char *osrc = src;
size_t nleft = dsize;
/* Copy as many bytes as will fit. */
if (nleft != 0)
{
while (--nleft != 0)
{
if ((*dst++ = *src++) == '\0')
{
break;
}
}
}
/* Not enough room in dst, add NUL and traverse rest of src. */
if (nleft == 0)
{
if (dsize != 0)
{
*dst = '\0'; /* NUL-terminate dst */
}
while (*src++)
{
;
}
}
return (src - osrc - 1); /* count does not include NUL */
}
#endif
/*****************************************************************************/
int
g_htoi(char *str)
+5
View File
@@ -296,6 +296,11 @@ int g_strcasecmp(const char *c1, const char *c2);
int g_strncasecmp(const char *c1, const char *c2, int len);
int g_atoi(const char *str);
/* Non-standard but useful functions */
#if !defined(HAVE_STRLCPY)
size_t strlcpy(char *dst, const char *src, size_t dsize);
#endif
/**
* Extends g_atoi(), Converts decimal and hexadecimal number String to integer
*