Coverage Report

Created: 2026-08-30 07:13

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
0
{
40
0
  struct printbuf *p;
41
42
0
  p = (struct printbuf *)calloc(1, sizeof(struct printbuf));
43
0
  if (!p)
44
0
    return NULL;
45
0
  p->size = 32;
46
0
  p->bpos = 0;
47
0
  if (!(p->buf = (char *)malloc(p->size)))
48
0
  {
49
0
    free(p);
50
0
    return NULL;
51
0
  }
52
0
  p->buf[0] = '\0';
53
0
  return p;
54
0
}
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
0
{
68
0
  char *t;
69
0
  int new_size;
70
71
0
  if (p->size >= min_size)
72
0
    return 0;
73
  /* Prevent signed integer overflows with large buffers. */
74
0
  if (min_size > INT_MAX - 8)
75
0
  {
76
0
    errno = EFBIG;
77
0
    return -1;
78
0
  }
79
0
  if (p->size > INT_MAX / 2)
80
0
    new_size = min_size + 8;
81
0
  else {
82
0
    new_size = p->size * 2;
83
0
    if (new_size < min_size + 8)
84
0
      new_size = min_size + 8;
85
0
  }
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
0
  if (!(t = (char *)realloc(p->buf, new_size)))
92
0
    return -1;
93
0
  p->size = new_size;
94
0
  p->buf = t;
95
0
  return 0;
96
0
}
97
98
int printbuf_memappend(struct printbuf *p, const char *buf, int size)
99
0
{
100
  /* Prevent signed integer overflows with large buffers. */
101
0
  if (size < 0 || size > INT_MAX - p->bpos - 1)
102
0
  {
103
0
    errno = EFBIG;
104
0
    return -1;
105
0
  }
106
0
  if (p->size <= p->bpos + size + 1)
107
0
  {
108
0
    int buf_offset = -1;
109
0
    const uintptr_t buf_addr = (uintptr_t)(const void *)buf;
110
0
    const uintptr_t p_buf_addr = (uintptr_t)(const void *)p->buf;
111
112
    /* realloc() invalidates source pointers into the old buffer. */
113
0
    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
0
    if (printbuf_extend(p, p->bpos + size + 1) < 0)
116
0
      return -1;
117
0
    if (buf_offset >= 0)
118
0
      buf = p->buf + buf_offset;
119
0
  }
120
0
  memmove(p->buf + p->bpos, buf, size);
121
0
  p->bpos += size;
122
0
  p->buf[p->bpos] = '\0';
123
0
  return size;
124
0
}
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
0
  if (pb->size < size_needed)
140
0
  {
141
0
    if (printbuf_extend(pb, size_needed) < 0)
142
0
      return -1;
143
0
  }
144
145
0
  if (pb->bpos < offset)
146
0
    memset(pb->buf + pb->bpos, '\0', offset - pb->bpos);
147
0
  memset(pb->buf + offset, charvalue, len);
148
0
  if (pb->bpos < size_needed)
149
0
    pb->bpos = size_needed;
150
151
0
  return 0;
152
0
}
153
154
int sprintbuf(struct printbuf *p, const char *msg, ...)
155
0
{
156
0
  va_list ap;
157
0
  char *t;
158
0
  int size;
159
0
  char buf[128];
160
161
  /* use stack buffer first */
162
0
  va_start(ap, msg);
163
0
  size = vsnprintf(buf, 128, msg, ap);
164
0
  va_end(ap);
165
  /* if string is greater than stack buffer, then use dynamic string
166
   * with vasprintf.  Note: some implementations of vsnprintf return -1
167
   * if output is truncated whereas some return the number of bytes that
168
   * would have been written - this code handles both cases.
169
   */
170
0
  if (size < 0 || size > 127)
171
0
  {
172
0
    va_start(ap, msg);
173
0
    if ((size = vasprintf(&t, msg, ap)) < 0)
174
0
    {
175
0
      va_end(ap);
176
0
      return -1;
177
0
    }
178
0
    va_end(ap);
179
0
    size = printbuf_memappend(p, t, size);
180
0
    free(t);
181
0
  }
182
0
  else
183
0
  {
184
0
    size = printbuf_memappend(p, buf, size);
185
0
  }
186
0
  return size;
187
0
}
188
189
void printbuf_reset(struct printbuf *p)
190
0
{
191
0
  p->buf[0] = '\0';
192
0
  p->bpos = 0;
193
0
}
194
195
void printbuf_free(struct printbuf *p)
196
0
{
197
0
  if (p)
198
0
  {
199
0
    free(p->buf);
200
0
    free(p);
201
0
  }
202
0
}