Skip to content

Commit

Permalink
Fix input_is_safe()
Browse files Browse the repository at this point in the history
Hooray for unit tests!
  • Loading branch information
Martin Lambers committed Oct 17, 2024
1 parent 4682d85 commit d31762c
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,24 @@ char *tty_input(pam_handle_t *pamh, const char *text, int echo_code)

int input_is_safe(const char *input, size_t max_length)
{
for (size_t i = 0; input[i]; i++)
{
if (i > max_length
/* Don't use isalnum() here because it is locale-dependent,
* and don't use isalnum_l() because it is not portable.
* Instead, hardcode a check for ASCII a-z A-Z 0-9. */
|| !( (input[i] >= 'a' && input[i] <= 'z')
|| (input[i] >= 'A' && input[i] <= 'Z')
|| (input[i] >= '0' && input[i] <= '9')))
{
return 0;
}
}
return 1;
size_t length = strlen(input);
if (length > max_length)
{
return 0;
}
for (size_t i = 0; i < length; i++)
{
/* Don't use isalnum() here because it is locale-dependent,
* and don't use isalnum_l() because it is not portable.
* Instead, hardcode a check for ASCII a-z A-Z 0-9. */
if (!( (input[i] >= 'a' && input[i] <= 'z')
|| (input[i] >= 'A' && input[i] <= 'Z')
|| (input[i] >= '0' && input[i] <= '9')))
{
return 0;
}
}
return 1;
}

void tty_output(pam_handle_t *pamh, const char *text)
Expand Down

0 comments on commit d31762c

Please sign in to comment.