/* Splits a user@host expression (one of those we accept on --machine= and similar). Returns NULL in
* each of the two return parameters if that part was left empty. */
+ assert(s);
+
const char *rhs = strchr(s, '@');
if (rhs) {
if (ret_user && rhs > s) {
if (!h)
return -ENOMEM;
}
- } else if (!isempty(s) && ret_host) {
- h = strdup(s);
- if (!h)
- return -ENOMEM;
+
+ } else {
+ if (isempty(s))
+ return -EINVAL;
+
+ if (ret_host) {
+ h = strdup(s);
+ if (!h)
+ return -ENOMEM;
+ }
}
if (ret_user)
}
TEST(split_user_at_host) {
- test_split_user_at_host_one("", NULL, NULL, 0);
+ ASSERT_ERROR(split_user_at_host("", NULL, NULL), EINVAL);
+
test_split_user_at_host_one("@", NULL, NULL, 1);
test_split_user_at_host_one("a", NULL, "a", 0);
test_split_user_at_host_one("a@b", "a", "b", 1);