Print this page
Address Robert's feedback

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/libsysdemangle/common/str.c
          +++ new/usr/src/lib/libsysdemangle/common/str.c
↓ open down ↓ 93 lines elided ↑ open up ↑
  94   94  }
  95   95  
  96   96  /*
  97   97   * ensure s has at least amt bytes free, resizing if necessary
  98   98   */
  99   99  static boolean_t
 100  100  str_reserve(str_t *s, size_t amt)
 101  101  {
 102  102          size_t newlen = s->str_len + amt;
 103  103  
      104 +        /* overflow check */
      105 +        if (newlen < s->str_len || newlen < amt)
      106 +                return (B_FALSE);
      107 +
 104  108          if ((amt > 0) && (s->str_len + amt <= s->str_size))
 105  109                  return (B_TRUE);
 106  110  
 107  111          size_t newsize = roundup(newlen, STR_CHUNK_SZ);
 108  112          void *temp;
 109  113  
 110  114          if (IS_REF(s)) {
 111  115                  temp = zalloc(s->str_ops, newsize);
 112  116                  if (temp == NULL)
 113  117                          return (B_FALSE);
↓ open down ↓ 42 lines elided ↑ open up ↑
 156  160  
 157  161          if (!str_reserve(dest, src->str_len))
 158  162                  return (B_FALSE);
 159  163  
 160  164          (void) memcpy(dest->str_s + dest->str_len, src->str_s, src->str_len);
 161  165          dest->str_len += src->str_len;
 162  166          return (B_TRUE);
 163  167  }
 164  168  
 165  169  boolean_t
 166      -str_append_c(str_t *s, int c)
      170 +str_append_c(str_t *s, char c)
 167  171  {
 168  172          if (!str_reserve(s, 1))
 169  173                  return (B_FALSE);
 170  174  
 171  175          s->str_s[s->str_len++] = c;
 172  176          return (B_TRUE);
 173  177  }
 174  178  
 175  179  boolean_t
 176  180  str_insert(str_t *s, size_t idx, const char *cstr, size_t cstrlen)
↓ open down ↓ 25 lines elided ↑ open up ↑
 202  206          if (idx == 0 && dest->str_s == NULL && IS_REF(src)) {
 203  207                  sysdem_ops_t *ops = dest->str_ops;
 204  208                  *dest = *src;
 205  209                  dest->str_ops = ops;
 206  210                  return (B_TRUE);
 207  211          }
 208  212  
 209  213          if (!str_reserve(dest, src->str_len))
 210  214                  return (B_FALSE);
 211  215  
 212      -        /* Unlike some programmers, *I* can read manpages. */
      216 +        /*
      217 +         * Shift the contents of dest over at the insertion point.  Since
      218 +         * src and dest ranges will overlap, and unlike some programmers,
      219 +         * *I* can read man pages - memmove() is the appropriate function
      220 +         * to this.
      221 +         */
 213  222          (void) memmove(dest->str_s + idx + src->str_len, dest->str_s + idx,
 214  223              dest->str_len - idx);
      224 +
      225 +        /*
      226 +         * However the content to insert does not overlap with the destination
      227 +         * so memcpy() is fine here.
      228 +         */
 215  229          (void) memcpy(dest->str_s + idx, src->str_s, src->str_len);
 216  230          dest->str_len += src->str_len;
 217  231  
 218  232          return (B_TRUE);
 219  233  }
 220  234  
 221  235  boolean_t
 222  236  str_erase(str_t *s, size_t pos, size_t len)
 223  237  {
 224  238          ASSERT3U(pos, <, s->str_len);
↓ open down ↓ 75 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX