strbuf.con commit Fix various memory leaks in http-push.c and http-walker.c (3a462bc)
   1#include "cache.h"
   2
   3/*
   4 * Used as the default ->buf value, so that people can always assume
   5 * buf is non NULL and ->buf is NUL terminated even for a freshly
   6 * initialized strbuf.
   7 */
   8char strbuf_slopbuf[1];
   9
  10void strbuf_init(struct strbuf *sb, size_t hint)
  11{
  12        sb->alloc = sb->len = 0;
  13        sb->buf = strbuf_slopbuf;
  14        if (hint)
  15                strbuf_grow(sb, hint);
  16}
  17
  18void strbuf_release(struct strbuf *sb)
  19{
  20        if (sb->alloc) {
  21                free(sb->buf);
  22                strbuf_init(sb, 0);
  23        }
  24}
  25
  26char *strbuf_detach(struct strbuf *sb, size_t *sz)
  27{
  28        char *res = sb->alloc ? sb->buf : NULL;
  29        if (sz)
  30                *sz = sb->len;
  31        strbuf_init(sb, 0);
  32        return res;
  33}
  34
  35void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
  36{
  37        strbuf_release(sb);
  38        sb->buf   = buf;
  39        sb->len   = len;
  40        sb->alloc = alloc;
  41        strbuf_grow(sb, 0);
  42        sb->buf[sb->len] = '\0';
  43}
  44
  45void strbuf_grow(struct strbuf *sb, size_t extra)
  46{
  47        if (sb->len + extra + 1 <= sb->len)
  48                die("you want to use way too much memory");
  49        if (!sb->alloc)
  50                sb->buf = NULL;
  51        ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
  52}
  53
  54void strbuf_rtrim(struct strbuf *sb)
  55{
  56        while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
  57                sb->len--;
  58        sb->buf[sb->len] = '\0';
  59}
  60
  61int strbuf_cmp(struct strbuf *a, struct strbuf *b)
  62{
  63        int cmp;
  64        if (a->len < b->len) {
  65                cmp = memcmp(a->buf, b->buf, a->len);
  66                return cmp ? cmp : -1;
  67        } else {
  68                cmp = memcmp(a->buf, b->buf, b->len);
  69                return cmp ? cmp : a->len != b->len;
  70        }
  71}
  72
  73void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
  74                                   const void *data, size_t dlen)
  75{
  76        if (pos + len < pos)
  77                die("you want to use way too much memory");
  78        if (pos > sb->len)
  79                die("`pos' is too far after the end of the buffer");
  80        if (pos + len > sb->len)
  81                die("`pos + len' is too far after the end of the buffer");
  82
  83        if (dlen >= len)
  84                strbuf_grow(sb, dlen - len);
  85        memmove(sb->buf + pos + dlen,
  86                        sb->buf + pos + len,
  87                        sb->len - pos - len);
  88        memcpy(sb->buf + pos, data, dlen);
  89        strbuf_setlen(sb, sb->len + dlen - len);
  90}
  91
  92void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
  93{
  94        strbuf_splice(sb, pos, 0, data, len);
  95}
  96
  97void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
  98{
  99        strbuf_splice(sb, pos, len, NULL, 0);
 100}
 101
 102void strbuf_add(struct strbuf *sb, const void *data, size_t len)
 103{
 104        strbuf_grow(sb, len);
 105        memcpy(sb->buf + sb->len, data, len);
 106        strbuf_setlen(sb, sb->len + len);
 107}
 108
 109void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
 110{
 111        strbuf_grow(sb, len);
 112        memcpy(sb->buf + sb->len, sb->buf + pos, len);
 113        strbuf_setlen(sb, sb->len + len);
 114}
 115
 116void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
 117{
 118        int len;
 119        va_list ap;
 120
 121        if (!strbuf_avail(sb))
 122                strbuf_grow(sb, 64);
 123        va_start(ap, fmt);
 124        len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
 125        va_end(ap);
 126        if (len < 0)
 127                die("your vsnprintf is broken");
 128        if (len > strbuf_avail(sb)) {
 129                strbuf_grow(sb, len);
 130                va_start(ap, fmt);
 131                len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
 132                va_end(ap);
 133                if (len > strbuf_avail(sb)) {
 134                        die("this should not happen, your snprintf is broken");
 135                }
 136        }
 137        strbuf_setlen(sb, sb->len + len);
 138}
 139
 140void strbuf_expand(struct strbuf *sb, const char *format,
 141                   const char **placeholders, expand_fn_t fn, void *context)
 142{
 143        for (;;) {
 144                const char *percent, **p;
 145
 146                percent = strchrnul(format, '%');
 147                strbuf_add(sb, format, percent - format);
 148                if (!*percent)
 149                        break;
 150                format = percent + 1;
 151
 152                for (p = placeholders; *p; p++) {
 153                        if (!prefixcmp(format, *p))
 154                                break;
 155                }
 156                if (*p) {
 157                        fn(sb, *p, context);
 158                        format += strlen(*p);
 159                } else
 160                        strbuf_addch(sb, '%');
 161        }
 162}
 163
 164size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
 165{
 166        size_t res;
 167
 168        strbuf_grow(sb, size);
 169        res = fread(sb->buf + sb->len, 1, size, f);
 170        if (res > 0) {
 171                strbuf_setlen(sb, sb->len + res);
 172        }
 173        return res;
 174}
 175
 176ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 177{
 178        size_t oldlen = sb->len;
 179
 180        strbuf_grow(sb, hint ? hint : 8192);
 181        for (;;) {
 182                ssize_t cnt;
 183
 184                cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
 185                if (cnt < 0) {
 186                        strbuf_setlen(sb, oldlen);
 187                        return -1;
 188                }
 189                if (!cnt)
 190                        break;
 191                sb->len += cnt;
 192                strbuf_grow(sb, 8192);
 193        }
 194
 195        sb->buf[sb->len] = '\0';
 196        return sb->len - oldlen;
 197}
 198
 199int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
 200{
 201        int ch;
 202
 203        strbuf_grow(sb, 0);
 204        if (feof(fp))
 205                return EOF;
 206
 207        strbuf_reset(sb);
 208        while ((ch = fgetc(fp)) != EOF) {
 209                if (ch == term)
 210                        break;
 211                strbuf_grow(sb, 1);
 212                sb->buf[sb->len++] = ch;
 213        }
 214        if (ch == EOF && sb->len == 0)
 215                return EOF;
 216
 217        sb->buf[sb->len] = '\0';
 218        return 0;
 219}
 220
 221int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
 222{
 223        int fd, len;
 224
 225        fd = open(path, O_RDONLY);
 226        if (fd < 0)
 227                return -1;
 228        len = strbuf_read(sb, fd, hint);
 229        close(fd);
 230        if (len < 0)
 231                return -1;
 232
 233        return len;
 234}