Skip to content

Commit 990a258

Browse files
bessmanAlexander Bessman
authored andcommitted
Set buffer pointer to null on return
1 parent 37a0791 commit 990a258

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/helpers/interval.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ enum Status INTERVAL_fetch_buffer(
260260
if (!g_buffer) { return E_RESOURCE_NOT_INITIALIZED; }
261261
*rets = (uint8_t *)g_buffer;
262262
*rets_size = g_n_buffer_items;
263-
g_n_buffer_items = 0;
264263
// Transport layer frees buffer.
264+
g_buffer = NULL;
265+
g_n_buffer_items = 0;
265266
return E_OK;
266267
}

src/instruments/oscilloscope.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ enum Status OSCILLOSCOPE_fetch_samples(
3838
return E_BAD_ARGSIZE;
3939
}
4040

41-
*rets = (uint8_t *)&g_buffer;
41+
// It's fine to call this even if g_buffer is NULL, i.e. before doing a
42+
// capture. It just won't return anything.
43+
*rets = (uint8_t *)g_buffer;
4244
*rets_size = g_n_samples * sizeof(*g_buffer);
43-
// Transport layer frees buffer after transmission is complete.
45+
// Transport layer frees g_buffer after transmission is complete.
46+
g_buffer = NULL;
4447
g_n_samples = 0;
4548

4649
return E_OK;
@@ -93,22 +96,21 @@ static enum Status capture(
9396
__attribute__((unused)) uint16_t *rets_size
9497
) {
9598
struct Input {
96-
uint8_t config;
9799
uint16_t samples;
98100
uint16_t delay;
101+
uint8_t config;
99102
uint8_t _pad[1];
100103
} *input = NULL;
101104

102-
if (args_size != sizeof(struct Input) - sizeof(input->_pad)) {
103-
return E_BAD_ARGSIZE;
104-
}
105-
105+
uint16_t const expected_size = sizeof(struct Input) - sizeof(input->_pad);
106+
if (args_size != expected_size) { return E_BAD_ARGSIZE; }
106107
input = *(struct Input **)args;
107108

108109
if (!input->samples) { return E_OK; }
109110
if (g_buffer) { return E_RESOURCE_BUSY; }
110111

111-
if ( !(g_buffer = malloc((input->samples) * sizeof(*g_buffer))) ) {
112+
// calloc to make sure we don't send garbage if capture is aborted early.
113+
if ( !(g_buffer = calloc(input->samples, sizeof(*g_buffer))) ) {
112114
return E_MEMORY_INSUFFICIENT;
113115
}
114116

@@ -157,25 +159,24 @@ enum Status OSCILLOSCOPE_capture_dma(
157159
__attribute__((unused)) uint16_t *rets_size
158160
) {
159161
struct Input {
160-
uint8_t config;
161162
uint16_t samples;
162163
uint16_t delay;
164+
uint8_t config;
163165
uint8_t _pad[1];
164166
} *input = NULL;
165167

166-
if (args_size != sizeof(struct Input) - sizeof(input->_pad)) {
167-
return E_BAD_ARGSIZE;
168-
}
169-
168+
uint16_t expected_size = sizeof(struct Input) - sizeof(input->_pad);
169+
if (args_size != expected_size) { return E_BAD_ARGSIZE; }
170170
input = *(struct Input **)args;
171171

172172
if (!input->samples) { return E_OK; }
173173
if (g_buffer) { return E_RESOURCE_BUSY; }
174174

175-
if ( !(g_buffer = malloc((input->samples) * sizeof(*g_buffer))) ) {
175+
if ( !(g_buffer = calloc(input->samples, sizeof(*g_buffer))) ) {
176176
return E_MEMORY_INSUFFICIENT;
177177
}
178178

179+
g_n_samples = input->samples;
179180
SetSAMPLES_REQUESTED(input->samples);
180181
SetDELAY(input->delay); // Wait DELAY / 8 us between samples.
181182

0 commit comments

Comments
 (0)