1
0
mirror of https://github.com/Yubico/yubico-pam.git synced 2025-02-21 15:54:41 +01:00

make sure filter is null terminated and return length including null

This commit is contained in:
Klas Lindfors 2015-03-04 10:48:09 +01:00
parent 686a47accf
commit 58382bf7fc
2 changed files with 16 additions and 13 deletions

View File

@ -126,13 +126,13 @@ static void test_load_chalresp_state(void) {
#endif /* HAVE_CR */ #endif /* HAVE_CR */
static void test_filter_printf(void) { static void test_filter_printf(void) {
assert(filter_result_len("meno %u", "doof", NULL) == 9); assert(filter_result_len("meno %u", "doof", NULL) == 10);
assert(filter_result_len("meno %u %u", "doof", NULL) == 14); assert(filter_result_len("meno %u %u", "doof", NULL) == 15);
assert(filter_result_len("%u meno %u", "doof", NULL) == 14); assert(filter_result_len("%u meno %u", "doof", NULL) == 15);
assert(filter_result_len("%u me %u no %u", "doof", NULL) == 20); assert(filter_result_len("%u me %u no %u", "doof", NULL) == 21);
assert(filter_result_len("meno %w %%u", "doof", NULL) == 13); assert(filter_result_len("meno %w %%u", "doof", NULL) == 14);
assert(filter_result_len("meno %w %%u meno", "doof", NULL) == 18); assert(filter_result_len("meno %w %%u meno", "doof", NULL) == 19);
assert(filter_result_len("meno ", "doof", NULL) == 5); assert(filter_result_len("meno ", "doof", NULL) == 6);
assert(!strcmp(filter_printf("meno %u", "doof"), "meno doof")); assert(!strcmp(filter_printf("meno %u", "doof"), "meno doof"));
assert(!strcmp(filter_printf("meno %u %u", "doof"), "meno doof doof")); assert(!strcmp(filter_printf("meno %u %u", "doof"), "meno doof doof"));

15
util.c
View File

@ -476,16 +476,16 @@ size_t filter_result_len(const char *filter, const char *user, char *output) {
len = part - filter; len = part - filter;
else else
len = strlen(filter); len = strlen(filter);
if (output != NULL) if (output)
{ {
strncpy(output, filter, len); strncpy(output, filter, len);
output += len; output += len;
} }
result += len; result += len;
filter += len + 2; filter += len + 2;
if(part != NULL) if(part)
{ {
if(output != NULL) if(output)
{ {
strncpy(output, user, strlen(user)); strncpy(output, user, strlen(user));
output += strlen(user); output += strlen(user);
@ -493,12 +493,15 @@ size_t filter_result_len(const char *filter, const char *user, char *output) {
result += strlen(user); result += strlen(user);
} }
} }
while(part != NULL); while(part);
return result;
if(output)
*output = '\0';
return(result + 1);
} }
char *filter_printf(const char *filter, const char *user) { char *filter_printf(const char *filter, const char *user) {
char *result = malloc(filter_result_len(filter, user, NULL) + 1); char *result = malloc(filter_result_len(filter, user, NULL));
filter_result_len(filter, user, result); filter_result_len(filter, user, result);
return result; return result;
} }