Skip to content

Commit f9c9c86

Browse files
committed
libioencode: allow NULL data parameter in iodecode()
Problem: There is no way to query the decoded length of data using iodecode() without unpacking the data itself, resulting in unnecessary allocation and memory copying. Allow data to be NULL when len is not. In this case unpacking the data is skipped, but the length is still assigned to to the lenp parameter.
1 parent 7083ce3 commit f9c9c86

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/common/libioencode/ioencode.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,16 @@ static int decode_data_base64 (char *src,
102102
{
103103
ssize_t rc;
104104
size_t size = base64_decoded_length (srclen);
105-
if (!(*datap = malloc (size)))
106-
return -1;
107-
if ((rc = base64_decode (*datap, size, src, srclen)) < 0)
108-
return -1;
109-
*lenp = rc;
105+
if (datap) {
106+
if (!(*datap = malloc (size)))
107+
return -1;
108+
if ((rc = base64_decode (*datap, size, src, srclen)) < 0)
109+
return -1;
110+
if (lenp)
111+
*lenp = rc;
112+
}
113+
else if (lenp)
114+
*lenp = size;
110115
return 0;
111116
}
112117

@@ -154,18 +159,21 @@ int iodecode (json_t *o,
154159
(*streamp) = stream;
155160
if (rankp)
156161
(*rankp) = rank;
157-
if (datap) {
158-
*datap = NULL;
162+
if (datap || lenp) {
163+
if (datap)
164+
*datap = NULL;
159165
if (data) {
160166
if (encoding && strcmp (encoding, "base64") == 0) {
161167
if (decode_data_base64 (data, len, datap, &bin_len) < 0)
162168
goto cleanup;
163169
}
164170
else {
165171
bin_len = len;
166-
if (!(*datap = malloc (bin_len)))
167-
goto cleanup;
168-
memcpy (*datap, data, bin_len);
172+
if (datap) {
173+
if (!(*datap = malloc (bin_len)))
174+
goto cleanup;
175+
memcpy (*datap, data, bin_len);
176+
}
169177
}
170178
}
171179
}

src/common/libioencode/ioencode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ json_t *ioencode (const char *stream,
2929
* - both data and EOF can be available
3030
* - if no data available, data set to NULL and len to 0
3131
* - data must be freed after return
32+
* - data can be NULL and len non-NULL to retrieve data length
3233
*/
3334
int iodecode (json_t *o,
3435
const char **stream,

0 commit comments

Comments
 (0)