Coverage Report

Created: 2025-06-13 06:29

/src/gdal/ogr/ogrsf_frmts/geojson/libjson/printbuf.c
Line
Count
Source (jump to first uncovered line)
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
 * (http://www.opensource.org/licenses/mit-license.php)
14
 */
15
16
#include "config.h"
17
18
#include <limits.h>
19
#include <stdio.h>
20
#include <stdlib.h>
21
#include <string.h>
22
23
#include "cpl_string.h"
24
25
#ifdef HAVE_STDARG_H
26
#include <stdarg.h>
27
#else /* !HAVE_STDARG_H */
28
#error Not enough var arg support!
29
#endif /* HAVE_STDARG_H */
30
31
#include "debug.h"
32
#include "printbuf.h"
33
34
static int printbuf_extend(struct printbuf *p, int min_size);
35
36
struct printbuf *printbuf_new(void)
37
0
{
38
0
  struct printbuf *p;
39
40
0
  p = (struct printbuf *)calloc(1, sizeof(struct printbuf));
41
0
  if (!p)
42
0
    return NULL;
43
0
  p->size = 32;
44
0
  p->bpos = 0;
45
0
  if (!(p->buf = (char *)malloc(p->size)))
46
0
  {
47
0
    free(p);
48
0
    return NULL;
49
0
  }
50
0
  p->buf[0] = '\0';
51
0
  return p;
52
0
}
53
54
/**
55
 * Extend the buffer p so it has a size of at least min_size.
56
 *
57
 * If the current size is large enough, nothing is changed.
58
 *
59
 * Note: this does not check the available space!  The caller
60
 *  is responsible for performing those calculations.
61
 */
62
static int printbuf_extend(struct printbuf *p, int min_size)
63
0
{
64
0
  char *t;
65
0
  int new_size;
66
67
0
  if (p->size >= min_size)
68
0
    return 0;
69
  /* Prevent signed integer overflows with large buffers. */
70
0
  if (min_size > INT_MAX - 8)
71
0
    return -1;
72
0
  if (p->size > INT_MAX / 2)
73
0
    new_size = min_size + 8;
74
0
  else {
75
0
    new_size = p->size * 2;
76
0
    if (new_size < min_size + 8)
77
0
      new_size = min_size + 8;
78
0
  }
79
#ifdef PRINTBUF_DEBUG
80
  MC_DEBUG("printbuf_memappend: realloc "
81
           "bpos=%d min_size=%d old_size=%d new_size=%d\n",
82
           p->bpos, min_size, p->size, new_size);
83
#endif /* PRINTBUF_DEBUG */
84
0
  if (!(t = (char *)realloc(p->buf, new_size)))
85
0
    return -1;
86
0
  p->size = new_size;
87
0
  p->buf = t;
88
0
  return 0;
89
0
}
90
91
int printbuf_memappend(struct printbuf *p, const char *buf, int size)
92
0
{
93
  /* Prevent signed integer overflows with large buffers. */
94
0
  if (size > INT_MAX - p->bpos - 1)
95
0
    return -1;
96
0
  if (p->size <= p->bpos + size + 1)
97
0
  {
98
0
    if (printbuf_extend(p, p->bpos + size + 1) < 0)
99
0
      return -1;
100
0
  }
101
0
  memcpy(p->buf + p->bpos, buf, size);
102
0
  p->bpos += size;
103
0
  p->buf[p->bpos] = '\0';
104
0
  return size;
105
0
}
106
107
int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
108
0
{
109
0
  int size_needed;
110
111
0
  if (offset == -1)
112
0
    offset = pb->bpos;
113
  /* Prevent signed integer overflows with large buffers. */
114
0
  if (len > INT_MAX - offset)
115
0
    return -1;
116
0
  size_needed = offset + len;
117
0
  if (pb->size < size_needed)
118
0
  {
119
0
    if (printbuf_extend(pb, size_needed) < 0)
120
0
      return -1;
121
0
  }
122
123
0
  memset(pb->buf + offset, charvalue, len);
124
0
  if (pb->bpos < size_needed)
125
0
    pb->bpos = size_needed;
126
127
0
  return 0;
128
0
}
129
130
/* Use CPLVASPrintf for portability issues */
131
int sprintbuf(struct printbuf *p, const char *msg, ...)
132
0
{
133
0
  va_list ap;
134
0
  char *t;
135
0
  int size, ret;
136
137
  /* user stack buffer first */
138
0
  va_start(ap, msg);
139
0
  size = CPLVASPrintf(&t, msg, ap);
140
0
  va_end(ap);
141
0
  if( size == -1 )
142
0
      return -1;
143
144
0
  if (strcmp(msg, "%f") == 0)
145
0
  {
146
0
      char* pszComma = strchr(t, ',');
147
0
      if (pszComma)
148
0
          *pszComma = '.';
149
0
  }
150
151
0
  ret = printbuf_memappend(p, t, size);
152
0
  CPLFree(t);
153
0
  return ret;
154
0
}
155
156
void printbuf_reset(struct printbuf *p)
157
0
{
158
0
  p->buf[0] = '\0';
159
0
  p->bpos = 0;
160
0
}
161
162
void printbuf_free(struct printbuf *p)
163
0
{
164
0
  if (p)
165
0
  {
166
0
    free(p->buf);
167
0
    free(p);
168
0
  }
169
0
}