Coverage Report

Created: 2026-10-16 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/json-c/printbuf.c
Line
Count
Source
1
/*
2
 * $Id: printbuf.c,v 1.5 2006/01/26 02:16:28 mclark Exp $
3
 *
4
 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5
 * Michael Clark <michael@metaparadigm.com>
6
 *
7
 * This library is free software; you can redistribute it and/or modify
8
 * it under the terms of the MIT license. See COPYING for details.
9
 *
10
 *
11
 * Copyright (c) 2008-2009 Yahoo! Inc.  All rights reserved.
12
 * The copyrights to the contents of this file are licensed under the MIT License
13
 * (https://www.opensource.org/licenses/mit-license.php)
14
 */
15
16
#include "config.h"
17
18
#include <errno.h>
19
#include <limits.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23
24
#ifdef HAVE_STDARG_H
25
#include <stdarg.h>
26
#else /* !HAVE_STDARG_H */
27
#error Not enough var arg support!
28
#endif /* HAVE_STDARG_H */
29
30
#include "debug.h"
31
#include "json_inttypes.h"
32
#include "printbuf.h"
33
#include "snprintf_compat.h"
34
#include "vasprintf_compat.h"
35
36
static int printbuf_extend(struct printbuf *p, int min_size);
37
38
struct printbuf *printbuf_new(void)
39
13.2k
{
40
13.2k
  struct printbuf *p;
41
42
13.2k
  p = (struct printbuf *)calloc(1, sizeof(struct printbuf));
43
13.2k
  if (!p)
44
0
    return NULL;
45
13.2k
  p->size = 32;
46
13.2k
  p->bpos = 0;
47
13.2k
  if (!(p->buf = (char *)malloc(p->size)))
48
0
  {
49
0
    free(p);
50
0
    return NULL;
51
0
  }
52
13.2k
  p->buf[0] = '\0';
53
13.2k
  return p;
54
13.2k
}
55
56
/**
57
 * Extend the buffer p so it has a size of at least min_size.
58
 *
59
 * If the current size is large enough, nothing is changed.
60
 *
61
 * If extension failed, errno is set to indicate the error.
62
 *
63
 * Note: this does not check the available space!  The caller
64
 *  is responsible for performing those calculations.
65
 */
66
static int printbuf_extend(struct printbuf *p, int min_size)
67
25.4k
{
68
25.4k
  char *t;
69
25.4k
  int new_size;
70
71
25.4k
  if (p->size >= min_size)
72
4.08k
    return 0;
73
  /* Prevent signed integer overflows with large buffers. */
74
21.3k
  if (min_size > INT_MAX - 8)
75
0
  {
76
0
    errno = EFBIG;
77
0
    return -1;
78
0
  }
79
21.3k
  if (p->size > INT_MAX / 2)
80
0
    new_size = min_size + 8;
81
21.3k
  else {
82
21.3k
    new_size = p->size * 2;
83
21.3k
    if (new_size < min_size + 8)
84
489
      new_size = min_size + 8;
85
21.3k
  }
86
#ifdef PRINTBUF_DEBUG
87
  MC_DEBUG("printbuf_extend: realloc "
88
           "bpos=%d min_size=%d old_size=%d new_size=%d\n",
89
           p->bpos, min_size, p->size, new_size);
90
#endif /* PRINTBUF_DEBUG */
91
21.3k
  if (!(t = (char *)realloc(p->buf, new_size)))
92
0
    return -1;
93
21.3k
  p->size = new_size;
94
21.3k
  p->buf = t;
95
21.3k
  return 0;
96
21.3k
}
97
98
int printbuf_memappend(struct printbuf *p, const char *buf, int size)
99
1.22M
{
100
  /* Prevent signed integer overflows with large buffers. */
101
1.22M
  if (size < 0 || size > INT_MAX - p->bpos - 1)
102
0
  {
103
0
    errno = EFBIG;
104
0
    return -1;
105
0
  }
106
1.22M
  if (p->size <= p->bpos + size + 1)
107
25.4k
  {
108
25.4k
    int buf_offset = -1;
109
25.4k
    const uintptr_t buf_addr = (uintptr_t)(const void *)buf;
110
25.4k
    const uintptr_t p_buf_addr = (uintptr_t)(const void *)p->buf;
111
112
    /* realloc() invalidates source pointers into the old buffer. */
113
25.4k
    if (buf_addr >= p_buf_addr && buf_addr - p_buf_addr < (uintptr_t)p->size)
114
0
      buf_offset = (int)(buf_addr - p_buf_addr);
115
25.4k
    if (printbuf_extend(p, p->bpos + size + 1) < 0)
116
0
      return -1;
117
25.4k
    if (buf_offset >= 0)
118
0
      buf = p->buf + buf_offset;
119
25.4k
  }
120
1.22M
  memmove(p->buf + p->bpos, buf, size);
121
1.22M
  p->bpos += size;
122
1.22M
  p->buf[p->bpos] = '\0';
123
1.22M
  return size;
124
1.22M
}
125
126
int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
127
0
{
128
0
  int size_needed;
129
130
0
  if (offset == -1)
131
0
    offset = pb->bpos;
132
  /* Prevent signed integer overflows with large buffers. */
133
0
  if (len < 0 || offset < -1 || len > INT_MAX - offset)
134
0
  {
135
0
    errno = EFBIG;
136
0
    return -1;
137
0
  }
138
0
  size_needed = offset + len;
139
  /* + 1 keeps the printbuf invariant that buf[bpos] is '\0' after every
140
   * operation; without it, reading pb->buf as a C string right after an
141
   * indent() consumes uninitialized bytes. */
142
0
  if (pb->size < size_needed + 1)
143
0
  {
144
0
    if (printbuf_extend(pb, size_needed + 1) < 0)
145
0
      return -1;
146
0
  }
147
148
0
  if (pb->bpos < offset)
149
0
    memset(pb->buf + pb->bpos, '\0', offset - pb->bpos);
150
0
  memset(pb->buf + offset, charvalue, len);
151
0
  if (pb->bpos < size_needed)
152
0
    pb->bpos = size_needed;
153
0
  pb->buf[pb->bpos] = '\0';
154
155
0
  return 0;
156
0
}
157
158
int sprintbuf(struct printbuf *p, const char *msg, ...)
159
0
{
160
0
  va_list ap;
161
0
  char *t;
162
0
  int size;
163
0
  char buf[128];
164
165
  /* use stack buffer first */
166
0
  va_start(ap, msg);
167
0
  size = vsnprintf(buf, 128, msg, ap);
168
0
  va_end(ap);
169
  /* if string is greater than stack buffer, then use dynamic string
170
   * with vasprintf.  Note: some implementations of vsnprintf return -1
171
   * if output is truncated whereas some return the number of bytes that
172
   * would have been written - this code handles both cases.
173
   */
174
0
  if (size < 0 || size > 127)
175
0
  {
176
0
    va_start(ap, msg);
177
0
    if ((size = vasprintf(&t, msg, ap)) < 0)
178
0
    {
179
0
      va_end(ap);
180
0
      return -1;
181
0
    }
182
0
    va_end(ap);
183
0
    size = printbuf_memappend(p, t, size);
184
0
    free(t);
185
0
  }
186
0
  else
187
0
  {
188
0
    size = printbuf_memappend(p, buf, size);
189
0
  }
190
0
  return size;
191
0
}
192
193
void printbuf_reset(struct printbuf *p)
194
209k
{
195
209k
  p->buf[0] = '\0';
196
209k
  p->bpos = 0;
197
209k
}
198
199
void printbuf_free(struct printbuf *p)
200
196k
{
201
196k
  if (p)
202
13.2k
  {
203
13.2k
    free(p->buf);
204
13.2k
    free(p);
205
13.2k
  }
206
196k
}