• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Alan-Jowett / cudd / 19143674264

06 Nov 2025 05:06PM UTC coverage: 32.995%. First build
19143674264

Pull #6

github

web-flow
Merge 8866d606b into ab3642766
Pull Request #6: Normalize memory allocation in C code to use ALLOC/REALLOC/FREE macros

0 of 7 new or added lines in 1 file covered. (0.0%)

9790 of 29671 relevant lines covered (33.0%)

318278.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/cstringstream.c
1
/**
2
  @file 
3

4
  @ingroup cstringstream
5

6
  @brief Simple string streams in C.
7

8
  @author Fabio Somenzi
9

10
  @copyright@parblock
11
  Copyright (c) 2014-2015, Regents of the University of Colorado
12

13
  All rights reserved.
14

15
  Redistribution and use in source and binary forms, with or without
16
  modification, are permitted provided that the following conditions
17
  are met:
18

19
  Redistributions of source code must retain the above copyright
20
  notice, this list of conditions and the following disclaimer.
21

22
  Redistributions in binary form must reproduce the above copyright
23
  notice, this list of conditions and the following disclaimer in the
24
  documentation and/or other materials provided with the distribution.
25

26
  Neither the name of the University of Colorado nor the names of its
27
  contributors may be used to endorse or promote products derived from
28
  this software without specific prior written permission.
29

30
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34
  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
35
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
36
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
40
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41
  POSSIBILITY OF SUCH DAMAGE.
42
  @endparblock
43

44
*/
45

46
#include <stdio.h>
47
#include <stdlib.h>
48
#include <string.h>
49
#include "util.h"
50
#include "cstringstream.h"
51

52
/**
53
 * @brief Type of a simple extensible string buffer.
54
 */
55
struct _cstringstream {
56
  size_t capacity; /**< elements allocated */
57
  size_t inUse;    /**< elements currently in use */
58
  char * data;     /**< actual data */
59
};
60

61
cstringstream newStringStream(void) {
×
62
  cstringstream ss;
NEW
63
  ss = ALLOC(struct _cstringstream, 1);
×
64
  if (!ss) return NULL;
×
65
  ss->capacity = 1; /* parsimonious */
×
66
  ss->inUse = 0;
×
NEW
67
  ss->data = ALLOC(char, ss->capacity);
×
68
  if (!ss->data) {
×
NEW
69
    FREE(ss);
×
70
    return NULL;
×
71
  }
72
  return ss;
×
73
}
74

75
void deleteStringStream(cstringstream ss) {
×
76
  if (ss) {
×
NEW
77
    FREE(ss->data);
×
NEW
78
    FREE(ss);
×
79
  }
80
}
×
81

82
int clearStringStream(cstringstream ss) {
×
83
  if (!ss) return -1;
×
84
  ss->inUse = 0;
×
85
  return 0;
×
86
}
87

88
cstringstream copyStringStream(const_cstringstream src) {
×
89
  cstringstream dest;
90
  if (!src) return 0;
×
91
  dest = newStringStream();
×
92
  if (!dest) return 0;
×
93
  if (resizeStringStream(dest, src->inUse)) {
×
94
    deleteStringStream(dest);
×
95
    return 0;
×
96
  }
97
  strncpy(dest->data, src->data, src->inUse);
×
98
  return dest;
×
99
}
100

101
int resizeStringStream(cstringstream ss, size_t newSize) {
×
102
  if (newSize > ss->capacity) {
×
103
    /* To avoid too many calls to realloc, we choose the larger of
104
     * twice the current size and the new requested size. */
105
    size_t newCapacity = 2 * ss->capacity;
×
106
    if (newCapacity < newSize)
×
107
      newCapacity = newSize;
×
NEW
108
    char * tmp = REALLOC(char, ss->data, newCapacity);
×
109
    /* If the allocation fails, leave the array alone. */
110
    if (!tmp) return -1;
×
111
    ss->data = tmp;
×
112
    ss->capacity = newCapacity;
×
113
  }
114
  /* Here we are guaranteed that newSize <= ss->capacity. */
115
  ss->inUse = newSize;
×
116
  return 0;
×
117
}
118

119
int sizeStringStream(const_cstringstream ss, size_t * num) {
×
120
  if (!ss || !num) return -1;
×
121
  *num = ss->inUse;
×
122
  return 0;
×
123
}
124

125
int getStringStream(const_cstringstream ss, size_t index, char * c) {
×
126
  if (!ss || !c || index >= ss->inUse) return -1;
×
127
  *c = ss->data[index];
×
128
  return 0;
×
129
}
130

131
int appendCharStringStream(cstringstream ss, char c) {
×
132
  if (!ss) return -1;
×
133
  if (resizeStringStream(ss, ss->inUse + 1)) return -1;
×
134
  /* Now we have space. */
135
  ss->data[ss->inUse-1] = c;
×
136
  return 0;
×
137
}
138

139
int appendStringStringStream(cstringstream ss, char const * s) {
×
140
  if (!ss) return -1;
×
141
  size_t len = strlen(s);
×
142
  if (resizeStringStream(ss, ss->inUse + len)) return -1;
×
143
  /* Now we have space. */
144
  strncpy(ss->data + ss->inUse - len, s, len); 
×
145
  return 0;
×
146
}
147

148
int appendIntStringStream(cstringstream ss, int d) {
×
149
  char str[256];
150
  if (!ss) return -1;
×
151
  sprintf(str, "%d", d);
×
152
  return appendStringStringStream(ss, str);
×
153
}
154

155
int appendUnsignedStringStream(cstringstream ss, unsigned u) {
×
156
  char str[256];
157
  if (!ss) return -1;
×
158
  sprintf(str, "%u", u);
×
159
  return appendStringStringStream(ss, str);
×
160
}
161

162
int appendLongStringStream(cstringstream ss, long ld) {
×
163
  char str[256];
164
  if (!ss) return -1;
×
165
  sprintf(str, "%ld", ld);
×
166
  return appendStringStringStream(ss, str);
×
167
}
168

169
int appendUnsignedLongStringStream(cstringstream ss, unsigned long lu) {
×
170
  char str[256];
171
  if (!ss) return -1;
×
172
  sprintf(str, "%lu", lu);
×
173
  return appendStringStringStream(ss, str);
×
174
}
175

176
int appendDoubleStringStream(cstringstream ss, double g) {
×
177
  char str[256];
178
  if (!ss) return -1;
×
179
  sprintf(str, "%g", g);
×
180
  return appendStringStringStream(ss, str);
×
181
}
182

183
int putStringStream(cstringstream ss, size_t index, char c) {
×
184
  if (!ss || index >= ss->inUse) return -1;
×
185
  ss->data[index] = c;
×
186
  return 0;
×
187
}
188

189
char * stringFromStringStream(const_cstringstream ss) {
×
190
  if (!ss) return 0;
×
NEW
191
  char * str = ALLOC(char, ss->inUse + 1);
×
192
  if (!str) return 0;
×
193
  strncpy(str, ss->data, ss->inUse);
×
194
  str[ss->inUse] = '\0';
×
195
  return str;
×
196
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc