Skip to content

Commit 1fa3040

Browse files
committed
context.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 55842bf commit 1fa3040

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

context.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,13 @@ char * iio_context_create_xml(const struct iio_context *ctx)
104104

105105
if (len > 0) {
106106
if (ctx->description) {
107-
iio_snprintf(str, len, "%s<context name=\"%s\" "
107+
ptr += iio_snprintf(str, len, "%s<context name=\"%s\" "
108108
"description=\"%s\" >",
109109
xml_header, ctx->name, ctx->description);
110110
} else {
111-
iio_snprintf(str, len, "%s<context name=\"%s\" >",
111+
ptr += iio_snprintf(str, len, "%s<context name=\"%s\" >",
112112
xml_header, ctx->name);
113113
}
114-
ptr = strrchr(str, '\0');
115114
len = eptr - ptr;
116115
}
117116

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

124123
for (i = 0; i < ctx->nb_devices; i++) {
125-
if (len > 0) {
126-
iio_strlcpy(ptr, devices[i], len);
124+
if (len > devices_len[i]) {
125+
memcpy(ptr, devices[i], devices_len[i]); /* Flawfinder: ignore */
127126
ptr += devices_len[i];
128127
len -= devices_len[i];
129128
}
@@ -134,7 +133,7 @@ char * iio_context_create_xml(const struct iio_context *ctx)
134133
free(devices_len);
135134

136135
if (len > 0) {
137-
iio_strlcpy(ptr, "</context>", len);
136+
ptr += iio_strlcpy(ptr, "</context>", len);
138137
len -= sizeof("</context>") - 1;
139138
}
140139

0 commit comments

Comments
 (0)