Changeset d8962c3 in freewrt


Ignore:
Timestamp:
Nov 9, 2006, 4:10:20 PM (19 years ago)
Author:
Thorsten Glaser <tg@…>
Children:
0ee1abf
Parents:
a3793e2
Message:

update from upstream - fixes bugs in strlcat() implementation in rare case

git-svn-id: svn://www.freewrt.org/trunk/freewrt@1104 afb5a338-a214-0410-bd46-81f09a774fd1

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/paxmirabilis/strlfun.c

    ra3793e2 rd8962c3  
    1 /* $MirOS: src/lib/libc/string/strlfun.c,v 1.5 2005/09/19 19:01:11 tg Exp $ */
    2 /* $OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */
    3 /* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */
     1/* $FreeWRT$ */
     2/* $MirOS: src/lib/libc/string/strlfun.c,v 1.10 2006/11/08 23:18:04 tg Exp $ */
    43
    54/*-
    6  * Copyright (c) 2004, 2005 Thorsten "mirabile" Glaser <tg@66h.42h.de>
    7  * Thanks to Bodo Eggert for optimisation hints
     5 * Copyright (c) 2006
     6 *      Thorsten Glaser <tg@mirbsd.de>
     7 *
     8 * Licensee is hereby permitted to deal in this work without restric-
     9 * tion, including unlimited rights to use, publicly perform, modify,
     10 * merge, distribute, sell, give away or sublicence, provided all co-
     11 * pyright notices above, these terms and the disclaimer are retained
     12 * in all redistributions or reproduced in accompanying documentation
     13 * or other materials provided with binary redistributions.
     14 *
     15 * Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind,
     16 * express, or implied, to the maximum extent permitted by applicable
     17 * law, without malicious intent or gross negligence; in no event may
     18 * licensor, an author or contributor be held liable for any indirect
     19 * or other damage, or direct damage except proven a consequence of a
     20 * direct error of said person and intended use of this work, loss or
     21 * other issues arising in any way out of its use, even if advised of
     22 * the possibility of such damage or existence of a defect.
     23 *-
     24 * The strlcat() code below has been written by Thorsten Glaser. Bodo
     25 * Eggert suggested optimising the strlcpy() code, originally written
     26 * by Todd C. Miller (see below), which was carried out by Th. Glaser
     27 * as well as writing wcslcat() and wcslcpy() equivalents.
     28 */
     29
     30#include <sys/types.h>
     31#if defined(_KERNEL) || defined(_STANDALONE)
     32#include <lib/libkern/libkern.h>
     33#undef HAVE_STRLCPY
     34#undef HAVE_STRLCAT
     35#else
     36#if defined(HAVE_CONFIG_H) && (HAVE_CONFIG_H != 0)
     37/* usually when packaged with third-party software */
     38#ifdef CONFIG_H_FILENAME
     39#include CONFIG_H_FILENAME
     40#else
     41#include "config.h"
     42#endif
     43#endif
     44extern size_t strlen(const char *);
     45#endif
     46
     47#ifndef __RCSID
     48#undef __IDSTRING
     49#undef __IDSTRING_CONCAT
     50#undef __IDSTRING_EXPAND
     51#if defined(__ELF__) && defined(__GNUC__)
     52#define __IDSTRING(prefix, string)                              \
     53        __asm__(".section .comment"                             \
     54        "\n     .ascii  \"@(\"\"#)" #prefix ": \""              \
     55        "\n     .asciz  \"" string "\""                         \
     56        "\n     .previous")
     57#else
     58#define __IDSTRING_CONCAT(l,p)          __LINTED__ ## l ## _ ## p
     59#define __IDSTRING_EXPAND(l,p)          __IDSTRING_CONCAT(l,p)
     60#define __IDSTRING(prefix, string)                              \
     61        static const char __IDSTRING_EXPAND(__LINE__,prefix) [] \
     62            __attribute__((used)) = "@(""#)" #prefix ": " string
     63#endif
     64#define __RCSID(x)              __IDSTRING(rcsid,x)
     65#endif
     66
     67#ifndef __predict_true
     68#define __predict_true(exp)     ((exp) != 0)
     69#endif
     70#ifndef __predict_false
     71#define __predict_false(exp)    ((exp) != 0)
     72#endif
     73
     74__RCSID("$MirOS: src/lib/libc/string/strlfun.c,v 1.10 2006/11/08 23:18:04 tg Exp $");
     75
     76size_t strlcat(char *, const char *, size_t);
     77size_t strlcpy(char *, const char *, size_t);
     78
     79#if !defined(HAVE_STRLCAT) || (HAVE_STRLCAT == 0)
     80/*
     81 * Appends src to string dst of size siz (unlike strncat, siz is the
     82 * full size of dst, not space left).  At most siz-1 characters
     83 * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
     84 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
     85 * If retval >= siz, truncation occurred.
     86 */
     87size_t
     88strlcat(char *dst, const char *src, size_t dlen)
     89{
     90        size_t n = 0, slen;
     91
     92        slen = strlen(src);
     93        while (__predict_true(n + 1 < dlen && dst[n] != '\0'))
     94                ++n;
     95        if (__predict_false(dlen == 0 || dst[n] != '\0'))
     96                return (dlen + slen);
     97        while (__predict_true((slen > 0) && (n < (dlen - 1)))) {
     98                dst[n++] = *src++;
     99                --slen;
     100        }
     101        dst[n] = '\0';
     102        return (n + slen);
     103}
     104#endif /* !HAVE_STRLCAT */
     105
     106/* $OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */
     107
     108/*-
    8109 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
    9110 *
     
    11112 * purpose with or without fee is hereby granted, provided that the above
    12113 * copyright notice and this permission notice appear in all copies.
    13  *
    14  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    16  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    20  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    21114 */
    22115
    23 #if !defined(_KERNEL) && !defined(_STANDALONE)
    24 #ifdef HAVE_CONFIG_H
    25 /* usually when packaged with third-party software */
    26 #include "config.h"
    27 #endif
    28 #include <sys/types.h>
    29 
    30 extern size_t strlen(const char *);
    31 
    32 #ifndef __RCSID
    33 #define __RCSID(x)      static const char __rcsid[] = (x)
    34 #endif
    35 
    36 __RCSID("$MirOS: src/lib/libc/string/strlfun.c,v 1.5 2005/09/19 19:01:11 tg Exp $");
    37 #else
    38 #include <lib/libkern/libkern.h>
    39 #undef HAVE_STRLCPY
    40 #undef HAVE_STRLCAT
    41 #endif
    42 
    43 size_t strlcat(char *, const char *, size_t);
    44 size_t strlcpy(char *, const char *, size_t);
    45 
    46 #ifndef HAVE_STRLCPY
     116#if !defined(HAVE_STRLCPY) || (HAVE_STRLCPY == 0)
    47117/*
    48118 * Copy src to string dst of size siz.  At most siz-1 characters
     
    55125        const char *s = src;
    56126
    57         if (!siz) goto traverse_src;
     127        if (__predict_false(siz == 0))
     128                goto traverse_src;
    58129
    59         /* Copy as many bytes as will fit */
    60         for (; --siz && (*dst++ = *s++); /* nothing */)
     130        /* copy as many chars as will fit */
     131        while (--siz && (*dst++ = *s++))
    61132                ;
    62133
    63         /* Not enough room in dst, add NUL and traverse rest of src */
    64         if (!siz) {
    65                 /* Save, since we've copied at max. (siz-1) characters */
    66                 *dst = '\0';    /* NUL-terminate dst */
    67 traverse_src:
     134        /* not enough room in dst */
     135        if (__predict_false(siz == 0)) {
     136                /* safe to NUL-terminate dst since we copied <= siz-1 chars */
     137                *dst = '\0';
     138 traverse_src:
     139                /* traverse rest of src */
    68140                while (*s++)
    69141                        ;
    70142        }
    71143
    72         return (s - src - 1);   /* count does not include NUL */
     144        /* count doesn't include NUL */
     145        return (s - src - 1);
    73146}
    74147#endif /* !HAVE_STRLCPY */
    75 
    76 #ifndef HAVE_STRLCAT
    77 /*
    78  * Appends src to string dst of size siz (unlike strncat, siz is the
    79  * full size of dst, not space left).  At most siz-1 characters
    80  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
    81  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
    82  * If retval >= siz, truncation occurred.
    83  */
    84 size_t
    85 strlcat(char *dst, const char *src, size_t siz)
    86 {
    87         char *d = dst;
    88         size_t dl, n = siz;
    89         const size_t sl = strlen(src);
    90 
    91         while (n-- && (*d++ != '\0'))
    92                 ;
    93         if (!++n && (*d != '\0'))
    94                 return strlen(src);
    95 
    96         dl = --d - dst;         /* original strlen(dst), max. siz-1 */
    97         n = siz - dl;
    98         dl += sl;
    99 
    100         if (!n--)
    101                 return dl;
    102 
    103         if (n > sl)
    104                 n = sl;         /* number of octets to copy */
    105         for (; n-- && (*d++ = *src++); /* nothing */)
    106                 ;
    107         *d = '\0';              /* NUL-terminate dst */
    108         return dl;
    109 }
    110 #endif /* !HAVE_STRLCAT */
Note: See TracChangeset for help on using the changeset viewer.