Skip to content

Commit 945407c

Browse files
committed
channel.c: optimize buidling xml function
try to use memcpy when we know the source length. memcpy will operate on word or double-word, unlike iio_strlcpy which operates on a byte basis. Mark these calls as Flawfinder ignore, since we check the dest buffer length to to the source length before we do a copy. Rather than itererate over the dest string to find the end (with strrchr), just keep track with math. We know the start, and we know the length (from the outputs of iio_snprintf & iio_strlcpy), so just add them. Signed-off-by: Robin Getz <[email protected]>
1 parent fde81b1 commit 945407c

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

channel.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -267,32 +267,29 @@ char * iio_channel_get_xml(const struct iio_channel *chn, size_t *length)
267267
eptr = str + len;
268268

269269
if (len > 0) {
270-
iio_snprintf(str, len, "<channel id=\"%s\"", chn->id);
271-
ptr = strrchr(str, '\0');
270+
ptr += iio_snprintf(str, len, "<channel id=\"%s\"", chn->id);
272271
len = eptr - ptr;
273272
}
274273

275274
if (chn->name && len > 0) {
276-
iio_snprintf(ptr, len, " name=\"%s\"", chn->name);
277-
ptr = strrchr(ptr, '\0');
275+
ptr += iio_snprintf(ptr, len, " name=\"%s\"", chn->name);
278276
len = eptr - ptr;
279277
}
280278

281279
if (len > 0) {
282-
iio_snprintf(ptr, len, " type=\"%s\" >", chn->is_output ? "output" : "input");
283-
ptr = strrchr(ptr, '\0');
280+
ptr += iio_snprintf(ptr, len, " type=\"%s\" >", chn->is_output ? "output" : "input");
284281
len = eptr - ptr;
285282
}
286283

287-
if (chn->is_scan_element && len > 0) {
288-
iio_strlcpy(ptr, scan_element, len);
284+
if (chn->is_scan_element && len > scan_element_len) {
285+
memcpy(ptr, scan_element, scan_element_len); /* Flawfinder: ignore */
289286
ptr += scan_element_len;
290287
len -= scan_element_len;
291288
}
292289

293290
for (i = 0; i < chn->nb_attrs; i++) {
294-
if (len > 0) {
295-
iio_strlcpy(ptr, attrs[i], len);
291+
if (len > attrs_len[i]) {
292+
memcpy(ptr, attrs[i], attrs_len[i]); /* Flawfinder: ignore */
296293
ptr += attrs_len[i];
297294
len -= attrs_len[i];
298295
}
@@ -304,10 +301,11 @@ char * iio_channel_get_xml(const struct iio_channel *chn, size_t *length)
304301
free(attrs_len);
305302

306303
if (len > 0) {
307-
iio_strlcpy(ptr, "</channel>", len);
308-
len -= sizeof("</channel>");
304+
ptr += iio_strlcpy(ptr, "</channel>", len);
305+
len -= sizeof("</channel>") -1;
309306
}
310-
*length = ptr - str + sizeof("</channel>") - 1;
307+
308+
*length = ptr - str;
311309

312310
if (len < 0) {
313311
IIO_ERROR("Internal libIIO error: iio_channel_get_xml str length isssue\n");

0 commit comments

Comments
 (0)