Skip to content

Commit

Permalink
strvec_join: free result on error and actually use separator char
Browse files Browse the repository at this point in the history
  • Loading branch information
millert committed Jan 28, 2021
1 parent 888f63a commit c9eff93
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
10 changes: 6 additions & 4 deletions plugins/sudoers/regress/unescape/check_unesc.c
Expand Up @@ -86,14 +86,15 @@ test_strlcpy_unescape(int *ntests_out, int *errors_out)
}

static void
test_strvec_join(int *ntests_out, int *errors_out)
test_strvec_join(char sep, int *ntests_out, int *errors_out)
{
int ntests = *ntests_out;
int errors = *errors_out;
char buf[64*1024 + 1], expected[64*1024 + 3];
char *argv[3], *result;

/* Test joining an argument vector while unescaping. */
/* Simulate: sudoedit -s '\' `perl -e 'print "A" x 65536'` */
memset(buf, 'A', sizeof(buf));
buf[sizeof(buf) - 1] = '\0';
argv[0] = "\\";
Expand All @@ -102,11 +103,11 @@ test_strvec_join(int *ntests_out, int *errors_out)

memset(expected, 'A', sizeof(expected));
expected[0] = '\\';
expected[1] = ' ';
expected[1] = sep;
expected[sizeof(expected) - 1] = '\0';

ntests++;
result = strvec_join(argv, ' ', strlcpy_unescape);
result = strvec_join(argv, sep, strlcpy_unescape);
if (result == NULL) {
sudo_warnx("%d: failed to join argument vector", ntests);
errors++;
Expand All @@ -132,7 +133,8 @@ main(int argc, char *argv[])
test_strlcpy_unescape(&ntests, &errors);

/* strvec_join test */
test_strvec_join(&ntests, &errors);
test_strvec_join(' ', &ntests, &errors);
test_strvec_join('\n', &ntests, &errors);

printf("%s: %d tests run, %d errors, %d%% success rate\n", getprogname(),
ntests, errors, (ntests - errors) * 100 / ntests);
Expand Down
9 changes: 7 additions & 2 deletions plugins/sudoers/strvec_join.c
Expand Up @@ -29,8 +29,12 @@

#include "sudoers.h"

/*
* Join a NULL-terminated array of strings using the specified separator
* character. The copy function must have strlcpy-like semantics.
*/
char *
strvec_join(char *const argv[], char ch, size_t (*cpy)(char *, const char *, size_t))
strvec_join(char *const argv[], char sep, size_t (*cpy)(char *, const char *, size_t))
{
char *dst, *result = NULL;
char *const *av;
Expand All @@ -48,11 +52,12 @@ strvec_join(char *const argv[], char ch, size_t (*cpy)(char *, const char *, siz
n = cpy(dst, *av, size);
if (n >= size) {
sudo_warnx(U_("internal error, %s overflow"), __func__);
free(result);
debug_return_ptr(NULL);
}
dst += n;
size -= n;
*dst++ = ' ';
*dst++ = sep;
size--;
}
*--dst = '\0';
Expand Down
2 changes: 1 addition & 1 deletion plugins/sudoers/sudoers.h
Expand Up @@ -459,6 +459,6 @@ void rcstr_delref(const char *s);
size_t strlcpy_unescape(char *dst, const char *src, size_t size);

/* strvec_join.c */
char *strvec_join(char *const argv[], char ch, size_t (*cpy)(char *, const char *, size_t));
char *strvec_join(char *const argv[], char sep, size_t (*cpy)(char *, const char *, size_t));

#endif /* SUDOERS_SUDOERS_H */

0 comments on commit c9eff93

Please sign in to comment.