check getpwnam_r()/getspnam_r()'s return code
Unlike getpwnam(), the reentrant variant is not documented to manipulate
errno, but instead it is supposed to return the error code directly.
Same for getspnam_r(). Make sure the code handles ERANGE correctly.
diff --git a/src/session_server_ssh.c b/src/session_server_ssh.c
index 2f77d26..ad89044 100644
--- a/src/session_server_ssh.c
+++ b/src/session_server_ssh.c
@@ -715,16 +715,16 @@
{
struct passwd *pwd = NULL;
char *mem;
+ int r = 0;
do {
- errno = 0;
- getpwnam_r(username, pwd_buf, *buf, *buf_size, &pwd);
+ r = getpwnam_r(username, pwd_buf, *buf, *buf_size, &pwd);
if (pwd) {
/* entry found */
break;
}
- if (errno == ERANGE) {
+ if (r == ERANGE) {
/* small buffer, enlarge */
*buf_size <<= 2;
mem = realloc(*buf, *buf_size);
@@ -734,7 +734,7 @@
}
*buf = mem;
}
- } while (errno == ERANGE);
+ } while (r == ERANGE);
return pwd;
}
@@ -744,11 +744,11 @@
{
struct spwd *spwd = NULL;
char *mem;
+ int r = 0;
do {
- errno = 0;
# ifndef __QNXNTO__
- getspnam_r(username, spwd_buf, *buf, *buf_size, &spwd);
+ r = getspnam_r(username, spwd_buf, *buf, *buf_size, &spwd);
# else
spwd = getspnam_r(username, spwd_buf, *buf, *buf_size);
# endif
@@ -757,7 +757,7 @@
break;
}
- if (errno == ERANGE) {
+ if (r == ERANGE) {
/* small buffer, enlarge */
*buf_size <<= 2;
mem = realloc(*buf, *buf_size);
@@ -767,7 +767,7 @@
}
*buf = mem;
}
- } while (errno == ERANGE);
+ } while (r == ERANGE);
return spwd;
}