Сначала мусор, утечка потом.
name, role и port — это указатели внутрь line. strtok просто расставляет NUL в этом буфере и возвращает адреса внутри него. Потом getline переиспользует тот же буфер для следующей строки. В итоге все хосты указывают на одну и ту же память, и вы видите то, что туда записала последняя строка. Если line перевыделится через realloc, будет ещё хуже, потому что старые указатели повиснут.
Каждой строке нужна своя копия:
inv->hosts[inv->count].name = strdup(name);
inv->hosts[inv->count].role = strdup(role);
Проверяйте на NULL и освобождайте обе, когда освобождаете inventory. Это граница времени жизни: line живёт одну итерацию, строки хостов живут столько же, сколько inv.
Утечка: ранние ветки return -1 (плохая строка, неудачный grow) пропускают free(line). Освобождается он только на «счастливом» пути. Либо освобождайте перед каждым return, либо делайте goto done на единую очистку.
getline выделяет буфер, так что владелец — вызывающий. На странице POSIX это прямо написано: https://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html
Переведено с английского·Показать оригиналGarbage first, the leak second.
name, role and port are pointers into line. strtok just drops NULs into that buffer and hands back addresses inside it. getline then reuses the same buffer for the next row. So every host ends up pointing at the same memory, and you see whatever the last line left there. If line gets realloc'd it's worse, because the old pointers dangle.
Each row needs its own copy:
inv->hosts[inv->count].name = strdup(name);
inv->hosts[inv->count].role = strdup(role);
Check for NULL, and free both when you free the inventory. That's the lifetime boundary: line lives for one iteration, the host strings live as long as inv.
The leak: the early return -1 paths (bad row, failed grow) skip free(line). Only the happy path frees it. Either free it before each return or goto done to a single cleanup.
getline allocates the buffer, so the caller owns it. The POSIX page spells that out: https://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html