This commit is contained in:
Joshua Rogers 2025-12-24 23:00:49 +11:00 committed by GitHub
commit 9b1dcf0b31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 4 deletions

View File

@ -108,10 +108,10 @@ int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
{
FILE *fp = NULL;
struct sockaddr_un addr;
int mybuffer, ret = -1, i, numbytes, fd;
int mybuffer, ret = -1, i, numbytes, fd = -1;
unsigned char tempbuf[255];
if (bytes > (int)sizeof(tempbuf))
if (bytes <= 0 || bytes > (int)sizeof(tempbuf))
return -1;
/* Make socket. */
@ -126,9 +126,8 @@ int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
#else
fd = socket(AF_UNIX, SOCK_STREAM, 0);
#endif
if (fd == -1 || (fp = fdopen(fd, "r+")) == NULL)
if (fd == -1)
return -1;
setbuf(fp, NULL);
/* Try to connect */
for (;;) {
@ -171,6 +170,14 @@ int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
}
}
/* Create stream only after a successful connect to avoid stale FILE* on fd swap. */
fp = fdopen(fd, "r+");
if (fp == NULL) {
close(fd);
return -1;
}
setbuf(fp, NULL);
/* Make request, see how many bytes we can get back. */
tempbuf[0] = 1;
tempbuf[1] = bytes;
@ -180,6 +187,9 @@ int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
goto err;
numbytes = tempbuf[0];
if (numbytes <= 0 || numbytes > bytes || numbytes > (int)sizeof(tempbuf))
goto err;
/* Which buffer are we using? */
mybuffer = buf == NULL;
if (mybuffer)
@ -196,6 +206,8 @@ int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
err:
if (fp != NULL)
fclose(fp);
else if (fd != -1)
close(fd);
return ret;
}