Browse Source

don't set errno or return an error when getspnam[_r] finds no entry

this case is specified as success with a null result, rather than an
error, and errno is not to be set on success.
master
Rich Felker 7 years ago
parent
commit
9db81b862d
  1. 3
      src/passwd/getspnam.c
  2. 9
      src/passwd/getspnam_r.c

3
src/passwd/getspnam.c

@ -8,10 +8,11 @@ struct spwd *getspnam(const char *name)
static char *line; static char *line;
struct spwd *res; struct spwd *res;
int e; int e;
int orig_errno = errno;
if (!line) line = malloc(LINE_LIM); if (!line) line = malloc(LINE_LIM);
if (!line) return 0; if (!line) return 0;
e = getspnam_r(name, &sp, line, LINE_LIM, &res); e = getspnam_r(name, &sp, line, LINE_LIM, &res);
if (e) errno = e; errno = e ? e : orig_errno;
return res; return res;
} }

9
src/passwd/getspnam_r.c

@ -67,6 +67,7 @@ int getspnam_r(const char *name, struct spwd *sp, char *buf, size_t size, struct
size_t k, l = strlen(name); size_t k, l = strlen(name);
int skip = 0; int skip = 0;
int cs; int cs;
int orig_errno = errno;
*res = 0; *res = 0;
@ -94,7 +95,11 @@ int getspnam_r(const char *name, struct spwd *sp, char *buf, size_t size, struct
} }
} else { } else {
f = fopen("/etc/shadow", "rbe"); f = fopen("/etc/shadow", "rbe");
if (!f) return errno; if (!f) {
if (errno != ENOENT && errno != ENOTDIR)
return errno;
return 0;
}
} }
pthread_cleanup_push(cleanup, f); pthread_cleanup_push(cleanup, f);
@ -113,6 +118,6 @@ int getspnam_r(const char *name, struct spwd *sp, char *buf, size_t size, struct
break; break;
} }
pthread_cleanup_pop(1); pthread_cleanup_pop(1);
if (rv) errno = rv; errno = rv ? rv : orig_errno;
return rv; return rv;
} }

Loading…
Cancel
Save