source: mainline/uspace/lib/c/generic/adt/measured_strings.c@ 0485135

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0485135 was f4c8a83f, checked in by Jakub Jermar <jakub@…>, 15 years ago

Do not leak memory buffers due ERROR_PROPAGATE.

  • Property mode set to 100644
File size: 11.5 KB
RevLine 
[21580dd]1/*
2 * Copyright (c) 2009 Lukas Mejdrech
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[2544442]29/** @addtogroup libc
[21580dd]30 * @{
31 */
32
33/** @file
[edba2b6f]34 * Character string with measured length implementation.
35 * @see measured_strings.h
[21580dd]36 */
37
[7390870]38#include <adt/measured_strings.h>
[21580dd]39#include <malloc.h>
40#include <mem.h>
41#include <unistd.h>
[c5b59ce]42#include <errno.h>
43#include <err.h>
[7390870]44#include <async.h>
[21580dd]45
[edba2b6f]46/** Creates a new measured string bundled with a copy of the given string
47 * itself as one memory block.
48 *
49 * If the measured string is being freed, whole memory block is freed.
50 * The measured string should be used only as a constant.
51 *
52 * @param[in] string The initial character string to be stored.
53 * @param[in] length The length of the given string without the terminating
[d2b1040]54 * zero ('\0') character. If the length is zero, the actual
55 * length is computed. The given length is used and
56 * appended with the terminating zero ('\0') character
[edba2b6f]57 * otherwise.
58 * @returns The new bundled character string with measured length.
59 * @returns NULL if there is not enough memory left.
60 */
61measured_string_ref
[d2b1040]62measured_string_create_bulk(const char *string, size_t length)
[edba2b6f]63{
[aadf01e]64 measured_string_ref new;
[21580dd]65
[edba2b6f]66 if (length == 0) {
67 while (string[length])
[d2b1040]68 length++;
[aadf01e]69 }
[edba2b6f]70 new = (measured_string_ref) malloc(sizeof(measured_string_t) +
71 (sizeof(char) * (length + 1)));
72 if (!new)
[aadf01e]73 return NULL;
[edba2b6f]74
[21580dd]75 new->length = length;
[aadf01e]76 new->value = ((char *) new) + sizeof(measured_string_t);
[21580dd]77 // append terminating zero explicitly - to be safe
[aadf01e]78 memcpy(new->value, string, new->length);
79 new->value[new->length] = '\0';
[edba2b6f]80
[21580dd]81 return new;
82}
83
[edba2b6f]84/** Copies the given measured string with separated header and data parts.
85 *
86 * @param[in] source The source measured string to be copied.
87 * @returns The copy of the given measured string.
88 * @returns NULL if the source parameter is NULL.
89 * @returns NULL if there is not enough memory left.
90 */
91measured_string_ref measured_string_copy(measured_string_ref source)
92{
[aadf01e]93 measured_string_ref new;
[21580dd]94
[edba2b6f]95 if (!source)
[aadf01e]96 return NULL;
[edba2b6f]97
[aadf01e]98 new = (measured_string_ref) malloc(sizeof(measured_string_t));
[edba2b6f]99 if (new) {
[aadf01e]100 new->value = (char *) malloc(source->length + 1);
[edba2b6f]101 if (new->value) {
[21580dd]102 new->length = source->length;
[aadf01e]103 memcpy(new->value, source->value, new->length);
104 new->value[new->length] = '\0';
[21580dd]105 return new;
106 }
[d2b1040]107 free(new);
[21580dd]108 }
[edba2b6f]109
[21580dd]110 return NULL;
111}
112
[edba2b6f]113/** Receives a measured strings array from a calling module.
114 *
115 * Creates the array and the data memory blocks.
116 * This method should be used only while processing IPC messages as the array
117 * size has to be negotiated in advance.
118 *
119 * @param[out] strings The received measured strings array.
120 * @param[out] data The measured strings data. This memory block stores the
121 * actual character strings.
122 * @param[in] count The size of the measured strings array.
123 * @returns EOK on success.
124 * @returns EINVAL if the strings or data parameter is NULL.
125 * @returns EINVAL if the count parameter is zero (0).
126 * @returns EINVAL if the sent array differs in size.
127 * @returns EINVAL if there is inconsistency in sent measured
128 * strings' lengths (should not occur).
129 * @returns ENOMEM if there is not enough memory left.
130 * @returns Other error codes as defined for the
131 * async_data_write_finalize() function.
132 */
133int
134measured_strings_receive(measured_string_ref *strings, char **data,
135 size_t count)
136{
[21580dd]137 ERROR_DECLARE;
138
[edba2b6f]139 size_t *lengths;
[aadf01e]140 size_t index;
141 size_t length;
[edba2b6f]142 char *next;
[aadf01e]143 ipc_callid_t callid;
[21580dd]144
[edba2b6f]145 if ((!strings) || (!data) || (count <= 0))
[21580dd]146 return EINVAL;
[edba2b6f]147
[aadf01e]148 lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
[edba2b6f]149 if (!lengths)
[aadf01e]150 return ENOMEM;
[edba2b6f]151
152 if ((!async_data_write_receive(&callid, &length)) ||
153 (length != sizeof(size_t) * (count + 1))) {
[aadf01e]154 free(lengths);
[21580dd]155 return EINVAL;
156 }
[d2b1040]157 if (ERROR_OCCURRED(async_data_write_finalize(callid, lengths,
158 length))) {
[aadf01e]159 free(lengths);
[21580dd]160 return ERROR_CODE;
161 }
[0402bda5]162
[aadf01e]163 *data = malloc(lengths[count]);
[d2b1040]164 if (!*data) {
[0402bda5]165 free(lengths);
[aadf01e]166 return ENOMEM;
167 }
168 (*data)[lengths[count] - 1] = '\0';
[edba2b6f]169
170 *strings = (measured_string_ref) malloc(sizeof(measured_string_t) *
171 count);
[d2b1040]172 if (!*strings) {
[aadf01e]173 free(lengths);
174 free(*data);
[21580dd]175 return ENOMEM;
176 }
[edba2b6f]177
178 next = *data;
[d2b1040]179 for (index = 0; index < count; index++) {
[aadf01e]180 (*strings)[index].length = lengths[index];
[edba2b6f]181 if (lengths[index] > 0) {
[d2b1040]182 if (!async_data_write_receive(&callid, &length) ||
[edba2b6f]183 (length != lengths[index])) {
[aadf01e]184 free(*data);
185 free(*strings);
186 free(lengths);
[21580dd]187 return EINVAL;
188 }
[f4c8a83f]189 if (ERROR_OCCURRED(async_data_write_finalize(callid,
190 next, lengths[index]))) {
191 free(*data);
192 free(*strings);
193 free(lengths);
194 return ERROR_CODE;
195 }
[aadf01e]196 (*strings)[index].value = next;
197 next += lengths[index];
[d2b1040]198 *next++ = '\0';
[edba2b6f]199 } else {
[aadf01e]200 (*strings)[index].value = NULL;
[21580dd]201 }
202 }
[edba2b6f]203
[aadf01e]204 free(lengths);
[21580dd]205 return EOK;
206}
207
[849ed54]208/** Computes the lengths of the measured strings in the given array.
[edba2b6f]209 *
210 * @param[in] strings The measured strings array to be processed.
211 * @param[in] count The measured strings array size.
212 * @returns The computed sizes array.
213 * @returns NULL if there is not enough memory left.
[849ed54]214 */
[edba2b6f]215static size_t *prepare_lengths(const measured_string_ref strings, size_t count)
216{
217 size_t *lengths;
[849ed54]218 size_t index;
219 size_t length;
220
221 lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
[edba2b6f]222 if (!lengths)
[849ed54]223 return NULL;
[edba2b6f]224
[849ed54]225 length = 0;
[d2b1040]226 for (index = 0; index < count; index++) {
[849ed54]227 lengths[index] = strings[index].length;
228 length += lengths[index] + 1;
229 }
230 lengths[count] = length;
231 return lengths;
232}
233
[edba2b6f]234/** Replies the given measured strings array to a calling module.
235 *
236 * This method should be used only while processing IPC messages as the array
237 * size has to be negotiated in advance.
238 *
239 * @param[in] strings The measured strings array to be transferred.
240 * @param[in] count The measured strings array size.
241 * @returns EOK on success.
242 * @returns EINVAL if the strings parameter is NULL.
243 * @returns EINVAL if the count parameter is zero (0).
244 * @returns EINVAL if the calling module does not accept the given
245 * array size.
246 * @returns EINVAL if there is inconsistency in sent measured
247 * strings' lengths (should not occur).
248 * @returns Other error codes as defined for the
249 * async_data_read_finalize() function.
250 */
251int measured_strings_reply(const measured_string_ref strings, size_t count)
252{
[21580dd]253 ERROR_DECLARE;
254
[edba2b6f]255 size_t *lengths;
[aadf01e]256 size_t index;
257 size_t length;
258 ipc_callid_t callid;
[21580dd]259
[edba2b6f]260 if ((!strings) || (count <= 0))
[21580dd]261 return EINVAL;
[edba2b6f]262
[aadf01e]263 lengths = prepare_lengths(strings, count);
[edba2b6f]264 if (!lengths)
[aadf01e]265 return ENOMEM;
[edba2b6f]266
[d2b1040]267 if (!async_data_read_receive(&callid, &length) ||
[edba2b6f]268 (length != sizeof(size_t) * (count + 1))) {
[aadf01e]269 free(lengths);
[21580dd]270 return EINVAL;
271 }
[d2b1040]272 if (ERROR_OCCURRED(async_data_read_finalize(callid, lengths, length))) {
[aadf01e]273 free(lengths);
[21580dd]274 return ERROR_CODE;
275 }
[aadf01e]276 free(lengths);
[edba2b6f]277
[d2b1040]278 for (index = 0; index < count; index++) {
[edba2b6f]279 if (strings[index].length > 0) {
[d2b1040]280 if (!async_data_read_receive(&callid, &length) ||
[edba2b6f]281 (length != strings[index].length)) {
[21580dd]282 return EINVAL;
283 }
[edba2b6f]284 ERROR_PROPAGATE(async_data_read_finalize(callid,
285 strings[index].value, strings[index].length));
[21580dd]286 }
287 }
[edba2b6f]288
[21580dd]289 return EOK;
290}
291
[edba2b6f]292/** Receives a measured strings array from another module.
293 *
294 * Creates the array and the data memory blocks.
295 * This method should be used only following other IPC messages as the array
296 * size has to be negotiated in advance.
297 *
298 * @param[in] phone The other module phone.
299 * @param[out] strings The returned measured strings array.
300 * @param[out] data The measured strings data. This memory block stores the
301 * actual character strings.
302 * @param[in] count The size of the measured strings array.
303 * @returns EOK on success.
304 * @returns EINVAL if the strings or data parameter is NULL.
305 * @returns EINVAL if the phone or count parameter is not positive.
306 * @returns EINVAL if the sent array differs in size.
307 * @returns ENOMEM if there is not enough memory left.
308 * @returns Other error codes as defined for the
309 * async_data_read_start() function.
310 */
311int
312measured_strings_return(int phone, measured_string_ref *strings, char **data,
313 size_t count)
314{
[21580dd]315 ERROR_DECLARE;
316
[edba2b6f]317 size_t *lengths;
[aadf01e]318 size_t index;
[edba2b6f]319 char *next;
[21580dd]320
[d2b1040]321 if ((phone < 0) || (!strings) || (!data) || (count <= 0))
[21580dd]322 return EINVAL;
[edba2b6f]323
[aadf01e]324 lengths = (size_t *) malloc(sizeof(size_t) * (count + 1));
[edba2b6f]325 if (!lengths)
[aadf01e]326 return ENOMEM;
[edba2b6f]327
328 if (ERROR_OCCURRED(async_data_read_start(phone, lengths,
329 sizeof(size_t) * (count + 1)))) {
[aadf01e]330 free(lengths);
[21580dd]331 return ERROR_CODE;
332 }
[edba2b6f]333
[aadf01e]334 *data = malloc(lengths[count]);
[d2b1040]335 if (!*data) {
[0402bda5]336 free(lengths);
[aadf01e]337 return ENOMEM;
338 }
[edba2b6f]339
340 *strings = (measured_string_ref) malloc(sizeof(measured_string_t) *
341 count);
[d2b1040]342 if (!*strings) {
[aadf01e]343 free(lengths);
344 free(*data);
[21580dd]345 return ENOMEM;
346 }
[edba2b6f]347
348 next = *data;
[d2b1040]349 for (index = 0; index < count; index++) {
[aadf01e]350 (*strings)[index].length = lengths[index];
[edba2b6f]351 if (lengths[index] > 0) {
[f4c8a83f]352 if (ERROR_OCCURRED(async_data_read_start(phone, next,
353 lengths[index]))) {
354 free(lengths);
355 free(data);
356 free(strings);
357 return ERROR_CODE;
358 }
[aadf01e]359 (*strings)[index].value = next;
360 next += lengths[index];
[d2b1040]361 *next++ = '\0';
[edba2b6f]362 } else {
[aadf01e]363 (*strings)[index].value = NULL;
[21580dd]364 }
365 }
[edba2b6f]366
[aadf01e]367 free(lengths);
[21580dd]368 return EOK;
369}
370
[edba2b6f]371/** Sends the given measured strings array to another module.
372 *
373 * This method should be used only following other IPC messages as the array
374 * size has to be negotiated in advance.
375 *
376 * @param[in] phone The other module phone.
377 * @param[in] strings The measured strings array to be transferred.
378 * @param[in] count The measured strings array size.
379 * @returns EOK on success.
380 * @returns EINVAL if the strings parameter is NULL.
381 * @returns EINVAL if the phone or count parameter is not positive.
382 * @returns Other error codes as defined for the
383 * async_data_write_start() function.
384 */
385int
386measured_strings_send(int phone, const measured_string_ref strings,
387 size_t count)
388{
[21580dd]389 ERROR_DECLARE;
390
[edba2b6f]391 size_t *lengths;
[aadf01e]392 size_t index;
[21580dd]393
[d2b1040]394 if ((phone < 0) || (!strings) || (count <= 0))
[21580dd]395 return EINVAL;
[edba2b6f]396
[aadf01e]397 lengths = prepare_lengths(strings, count);
[edba2b6f]398 if (!lengths)
[aadf01e]399 return ENOMEM;
[edba2b6f]400
401 if (ERROR_OCCURRED(async_data_write_start(phone, lengths,
402 sizeof(size_t) * (count + 1)))) {
[aadf01e]403 free(lengths);
[21580dd]404 return ERROR_CODE;
405 }
[edba2b6f]406
[aadf01e]407 free(lengths);
[edba2b6f]408
[d2b1040]409 for (index = 0; index < count; index++) {
[edba2b6f]410 if (strings[index].length > 0) {
411 ERROR_PROPAGATE(async_data_write_start(phone,
412 strings[index].value, strings[index].length));
[21580dd]413 }
414 }
[edba2b6f]415
[21580dd]416 return EOK;
417}
418
419/** @}
420 */
421
Note: See TracBrowser for help on using the repository browser.