From b4e3bdd69231be854af1032a3c8976fd67e35b5d Mon Sep 17 00:00:00 2001 From: Charles Longeau Date: Sat, 22 Nov 2008 17:01:27 +0000 Subject: [PATCH] * Replace all str{cpy,cat} functions with strl{cpy,cat} * Ensure that strl{cpy,cat} are included in libmagic, as needed. ok Christos --- ChangeLog | 8 ++++++- configure.ac | 2 +- src/compress.c | 4 ++-- src/file.h | 9 +++++++- src/fsmagic.c | 8 ++++--- src/magic.c | 7 ++++--- src/strlcat.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/strlcpy.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 137 insertions(+), 11 deletions(-) create mode 100644 src/strlcat.c create mode 100644 src/strlcpy.c diff --git a/ChangeLog b/ChangeLog index a756bdd4..4e6ddf50 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2008-11-09 20:42 Charles Longeau + + * Replace all str{cpy,cat} functions with strl{cpy,cat} + * Ensure that strl{cpy,cat} are included in libmagic, + as needed. + 2008-11-06 18:18 Christos Zoulas * Handle ID3 format files. @@ -6,7 +12,7 @@ * Fix --mime, --mime-type and --mime-encoding under new scheme. - * Rename "ascii" to "text" and add "encoding" test. + * Rename "ascii" to "text" and add "encoding" test. * Return a precise ("utf-16le" or "utf-16be") MIME charset for UTF-16. diff --git a/configure.ac b/configure.ac index c2d5068e..6af5f53d 100644 --- a/configure.ac +++ b/configure.ac @@ -141,7 +141,7 @@ dnl Checks for functions AC_CHECK_FUNCS(mmap strerror strndup strtoul mbrtowc mkstemp utimes utime wcwidth strtof) dnl Provide implementation of some required functions if necessary -AC_REPLACE_FUNCS(getopt_long asprintf vasprintf) +AC_REPLACE_FUNCS(getopt_long asprintf vasprintf strlcpy strlcat) dnl Checks for libraries AC_CHECK_LIB(z,gzopen) diff --git a/src/compress.c b/src/compress.c index e62cf2c4..3c82f278 100644 --- a/src/compress.c +++ b/src/compress.c @@ -35,7 +35,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: compress.c,v 1.57 2008/07/16 18:00:57 christos Exp $") +FILE_RCSID("@(#)$File: compress.c,v 1.58 2008/11/04 16:38:28 christos Exp $") #endif #include "magic.h" @@ -237,7 +237,7 @@ file_pipe2file(struct magic_set *ms, int fd, const void *startbuf, char buf[4096]; int r, tfd; - (void)strcpy(buf, "/tmp/file.XXXXXX"); + (void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf); #ifndef HAVE_MKSTEMP { char *ptr = mktemp(buf); diff --git a/src/file.h b/src/file.h index 897ed569..d616b265 100644 --- a/src/file.h +++ b/src/file.h @@ -27,7 +27,7 @@ */ /* * file.h - definitions for file(1) program - * @(#)$File: file.h,v 1.115 2008/11/06 23:22:54 christos Exp $ + * @(#)$File: file.h,v 1.116 2008/11/07 18:57:28 christos Exp $ */ #ifndef __file_h__ @@ -411,6 +411,13 @@ int vasprintf(char **, const char *, va_list); int asprintf(char **ptr, const char *format_string, ...); #endif +#ifndef HAVE_STRLCPY +size_t strlcpy(char *dst, const char *src, size_t siz); +#endif +#ifndef HAVE_STRLCAT +size_t strlcat(char *dst, const char *src, size_t siz); +#endif + #if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK) #define QUICK #endif diff --git a/src/fsmagic.c b/src/fsmagic.c index 30dbf5bd..96b5375d 100644 --- a/src/fsmagic.c +++ b/src/fsmagic.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: fsmagic.c,v 1.56 2008/11/07 17:25:59 christos Exp $") +FILE_RCSID("@(#)$File: fsmagic.c,v 1.57 2008/11/07 18:57:28 christos Exp $") #endif /* lint */ #include "magic.h" @@ -293,9 +293,11 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb) return -1; return 1; } - (void)strcpy(buf2, fn); /* take dir part */ + /* take dir part */ + (void)strlcpy(buf2, fn, sizeof buf2); buf2[tmp - fn + 1] = '\0'; - (void)strcat(buf2, buf); /* plus (rel) link */ + /* plus (rel) link */ + (void)strlcat(buf2, buf, sizeof buf2); tmp = buf2; } if (stat(tmp, &tstatbuf) < 0) diff --git a/src/magic.c b/src/magic.c index 00f8289f..ed1a1ff5 100644 --- a/src/magic.c +++ b/src/magic.c @@ -28,7 +28,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: magic.c,v 1.55 2008/11/04 16:38:28 christos Exp $") +FILE_RCSID("@(#)$File: magic.c,v 1.56 2008/11/06 21:17:45 rrt Exp $") #endif /* lint */ #include "magic.h" @@ -303,8 +303,9 @@ file_or_fd(struct magic_set *ms, const char *inname, int fd) if ((fd = open(inname, flags)) < 0) { #ifdef __CYGWIN__ /* FIXME: Do this with EXEEXT from autotools */ - char *tmp = alloca(strlen(inname) + 5); - (void)strcat(strcpy(tmp, inname), ".exe"); + size_t len = strlen(inname) + 5; + char *tmp = alloca(len); + (void)strlcat(strlcpy(tmp, inname, len), ".exe", len); if ((fd = open(tmp, flags)) < 0) { #endif if (unreadable_info(ms, sb.st_mode, diff --git a/src/strlcat.c b/src/strlcat.c new file mode 100644 index 00000000..3fc2dcbf --- /dev/null +++ b/src/strlcat.c @@ -0,0 +1,57 @@ +/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */ + +/* + * Copyright (c) 1998 Todd C. Miller + * + * 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. + */ + +/* OPENBSD ORIGINAL: lib/libc/string/strlcat.c */ + +#include +#include + +/* + * Appends src to string dst of size siz (unlike strncat, siz is the + * full size of dst, not space left). At most siz-1 characters + * will be copied. Always NUL terminates (unless siz <= strlen(dst)). + * Returns strlen(src) + MIN(siz, strlen(initial dst)). + * If retval >= siz, truncation occurred. + */ +size_t +strlcat(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + size_t dlen; + + /* Find the end of dst and adjust bytes left but don't go past end */ + while (n-- != 0 && *d != '\0') + d++; + dlen = d - dst; + n = siz - dlen; + + if (n == 0) + return(dlen + strlen(s)); + while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; + } + s++; + } + *d = '\0'; + + return(dlen + (s - src)); /* count does not include NUL */ +} diff --git a/src/strlcpy.c b/src/strlcpy.c new file mode 100644 index 00000000..224e1393 --- /dev/null +++ b/src/strlcpy.c @@ -0,0 +1,53 @@ +/* $OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */ + +/* + * Copyright (c) 1998 Todd C. Miller + * + * 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. + */ + +/* OPENBSD ORIGINAL: lib/libc/string/strlcpy.c */ + +#include +#include + +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) { + do { + if ((*d++ = *s++) == 0) + break; + } while (--n != 0); + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return(s - src - 1); /* count does not include NUL */ +} -- 2.40.0