Changeset 347


Ignore:
Timestamp:
04/07/15 04:20:53 (10 years ago)
Author:
Maciej Komosinski
Message:

Using memmove instead of memcpy because overlapping source/destination can happen in SString copy/append

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/util/sstring.cpp

    r286 r347  
    8080if (chlen<size)
    8181        {
    82         memcpy(txt,ch,chlen);
     82        memmove(txt,ch,chlen);
    8383        }
    8484else
     
    9797void SBuf::append(const char *ch,int chlen)
    9898{ // doesn't check anything!
    99 memcpy(txt+used,ch,chlen);
     99memmove(txt+used,ch,chlen);
    100100used+=chlen;
    101101txt[used]=0;
     
    108108txt=(char*)realloc(txt,needed+1);
    109109size=needed;
    110 }
    111 
    112 //////////////////////////////////////////////////
    113 // to be moved somewhere else?
    114 // public domain source: http://isthe.com/chongo/src/fnv
    115 typedef uint32_t Fnv32_t;
    116 
    117 #define FNV_32_PRIME ((Fnv32_t)0x01000193)
    118 #define FNV1_32_INIT ((Fnv32_t)0x811c9dc5)
    119 #define FNV1_32A_INIT FNV1_32_INIT
    120 
    121 Fnv32_t fnv_32a_buf(void *buf, size_t len, Fnv32_t hval)
    122 {
    123     unsigned char *bp = (unsigned char *)buf;   /* start of buffer */
    124     unsigned char *be = bp + len;               /* beyond end of buffer */
    125 
    126     while (bp < be) {
    127 
    128         /* xor the bottom with the current octet */
    129         hval ^= (Fnv32_t)*bp++;
    130 
    131         /* multiply by the 32 bit FNV magic prime mod 2^32 */
    132 #if defined(NO_FNV_GCC_OPTIMIZATION)
    133         hval *= FNV_32_PRIME;
    134 #else
    135         hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
    136 #endif
    137 
    138     }
    139 
    140     /* return our new hash value */
    141     return hval;
    142 }
    143 //////////////////////////////////////////////////
    144 
    145 uint32_t SBuf::hash() const
    146 {
    147 return fnv_32a_buf(txt,used,FNV1_32A_INIT);
    148110}
    149111
     
    308270detachEmpty(chlen);
    309271REF_UNLOCK;
    310 memcpy(buf->txt,ch,chlen);
     272memmove(buf->txt,ch,chlen);
    311273buf->txt[chlen]=0; buf->used=chlen;
    312274}
     
    463425
    464426#endif //#ifdef SSTRING_SIMPLE
     427
     428//////////////////////////////////////////////////
     429// to be moved somewhere else?
     430// public domain source: http://isthe.com/chongo/src/fnv
     431typedef uint32_t Fnv32_t;
     432
     433#define FNV_32_PRIME ((Fnv32_t)0x01000193)
     434#define FNV1_32_INIT ((Fnv32_t)0x811c9dc5)
     435#define FNV1_32A_INIT FNV1_32_INIT
     436
     437Fnv32_t fnv_32a_buf(void *buf, size_t len, Fnv32_t hval)
     438{
     439    unsigned char *bp = (unsigned char *)buf;   /* start of buffer */
     440    unsigned char *be = bp + len;               /* beyond end of buffer */
     441
     442    while (bp < be) {
     443
     444        /* xor the bottom with the current octet */
     445        hval ^= (Fnv32_t)*bp++;
     446
     447        /* multiply by the 32 bit FNV magic prime mod 2^32 */
     448#if defined(NO_FNV_GCC_OPTIMIZATION)
     449        hval *= FNV_32_PRIME;
     450#else
     451        hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24);
     452#endif
     453
     454    }
     455
     456    /* return our new hash value */
     457    return hval;
     458}
     459//////////////////////////////////////////////////
     460
     461#ifdef SSTRING_SIMPLE
     462uint32_t SString::hash() const
     463{
     464return fnv_32a_buf(txt,used,FNV1_32A_INIT);
     465}
     466#else
     467uint32_t SBuf::hash() const
     468{
     469return fnv_32a_buf(txt,used,FNV1_32A_INIT);
     470}
     471#endif
Note: See TracChangeset for help on using the changeset viewer.