nss_cache getpwent loop runs forever if getpwnam in body
- Dominant language
- C
- Stars
- 63
- Forks
- 29
- Avg merge
- 3m
- Merged PRs (30d)
- 4
Description
(Note: This is a copy of a Google-internal issue written by Stan Shebs, who's now retired; unfortunately I don't have additional context.)
Calling `getpwnam()` inside of a `getpwent()` loop causes it to run forever, if `nss_cache` is in use:
```
while ((pw = getpwent()) != NULL)
{
pw2 = getpwnam("anyname");
}
```
The problem is that `getpwnam` closes `/etc/passwd.cache`, so `getpwent()` then has to open and start reading from the
beginning again. The `nss_files` module has a bit of special code to prevent this, `nss_cache` will need something like it.
----
The attached file takes an argument, so you can see "bad files" work, and "bad cache" hang:
```
#include
#include
int
main(int argc, char **argv)
{
char *serviceline = "cache";
struct passwd *pw, *pw2;
if (argc > 1)
serviceline = argv[1];
__nss_configure_lookup("passwd", serviceline);
setpwent();
while ((pw = getpwent()) != NULL)
{
pw2 = getpwnam("anyname");
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.