[libvirt] [PATCH] tests: dynamically replace dnsmasq path
Eric Blake
eblake at redhat.com
Mon Jan 30 19:18:15 UTC 2012
On 01/30/2012 10:13 AM, Philipp Hahn wrote:
> The path to the dnsmasq binary can be configured while in the test data
> the path is hard-coded to /usr/bin/. This break the test suite if a the
> binary is located in a different location, like /usr/local/sbin/.
>
> Replace the hard coded path in the test data by a token, which is
> dynamically replaced in networkxml2argvtest with the configured path
> after the test data has been loaded.
Cool hack!
>
> (Another option would have been to modify configure.ac to generate the
> test data during configure, but I do not know of an easy way do trick
> configure into mass-generate those test files without listing every
> single one, which I consider less flexible.)
Agreed with your reasoning and choice of implementation to do the
replacement at test run rather than configure time. However,
> +++ b/tests/networkxml2argvtest.c
> @@ -15,6 +15,45 @@
> #include "memory.h"
> #include "network/bridge_driver.h"
>
> +/* Replace all occurrences of @token in @buf by @replacement and adjust size of
> + * @buf accordingly. Returns 0 on success and -1 on out-of-memory errors. */
> +static int replaceTokens(char **buf, const char *token, const char *replacement) {
> + char *token_start, *token_end;
> + size_t old_len, new_len, rest_len;
> + void *tmp;
> + const size_t token_len = strlen(token);
> + const size_t replacement_len = strlen(replacement);
> + const int diff = replacement_len - token_len;
> +
> + old_len = rest_len = strlen(*buf) + 1;
> + token_end = *buf;
> + for (;;) {
> + token_start = strstr(token_end, token);
> + if (token_start == NULL)
> + break;
> + rest_len -= token_start + token_len - token_end;
> + token_end = token_start + token_len;
> + new_len = old_len + diff;
> + if (diff > 0) {
> + tmp = realloc((void *)*buf, new_len);
We should not be using realloc in this file, but should be using
VIR_RESIZE_N or similar.
> + if (tmp == NULL)
> + return -1;
> + *buf = tmp;
> + }
> + if (diff != 0)
> + memmove(token_end + diff, token_end, rest_len);
This must be memmove,
> + memmove(token_start, replacement, replacement_len);
But this would be more efficient as memcpy, since replacement (better
not) overlap with token_start.
> + if (diff < 0) {
> + tmp = realloc((void *)*buf, new_len);
No need to downsize the allocation - it's okay to leave junk in the tail
end of the buffer, as long as we have a trailing NUL to stop us in time.
--
Eric Blake eblake at redhat.com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 620 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20120130/87ae8ec3/attachment-0001.sig>
More information about the libvir-list
mailing list