Skip to content

Commit

Permalink
context.c: optimize buidling xml function
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
rgetz committed Apr 22, 2020
1 parent 55842bf commit 1fa3040
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions context.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,13 @@ char * iio_context_create_xml(const struct iio_context *ctx)

if (len > 0) {
if (ctx->description) {
iio_snprintf(str, len, "%s<context name=\"%s\" "
ptr += iio_snprintf(str, len, "%s<context name=\"%s\" "
"description=\"%s\" >",
xml_header, ctx->name, ctx->description);
} else {
iio_snprintf(str, len, "%s<context name=\"%s\" >",
ptr += iio_snprintf(str, len, "%s<context name=\"%s\" >",
xml_header, ctx->name);
}
ptr = strrchr(str, '\0');
len = eptr - ptr;
}

Expand All @@ -122,8 +121,8 @@ char * iio_context_create_xml(const struct iio_context *ctx)
}

for (i = 0; i < ctx->nb_devices; i++) {
if (len > 0) {
iio_strlcpy(ptr, devices[i], len);
if (len > devices_len[i]) {
memcpy(ptr, devices[i], devices_len[i]); /* Flawfinder: ignore */
ptr += devices_len[i];
len -= devices_len[i];
}
Expand All @@ -134,7 +133,7 @@ char * iio_context_create_xml(const struct iio_context *ctx)
free(devices_len);

if (len > 0) {
iio_strlcpy(ptr, "</context>", len);
ptr += iio_strlcpy(ptr, "</context>", len);
len -= sizeof("</context>") - 1;
}

Expand Down

0 comments on commit 1fa3040

Please sign in to comment.