Fix the insufficient output buffer in SIOCGIFCONF ioctl test

This commit is contained in:
He Sun 2020-12-02 09:52:26 +08:00 committed by Zongmin.Gu
parent 9809d81c4e
commit 5285e3b55d

@ -6,6 +6,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
@ -222,7 +223,10 @@ int test_sgx_ioctl_SGXIOC_CREATE_AND_VERIFY_REPORT(void) {
int test_ioctl_SIOCGIFCONF(void) { int test_ioctl_SIOCGIFCONF(void) {
struct ifreq *req; struct ifreq *req;
struct ifconf conf; struct ifconf conf;
char buf[CONFIG_SIZE]; char *buf = (char *)malloc(CONFIG_SIZE);
if (buf == NULL) {
THROW_ERROR("malloc failed");
}
memset(buf, 0, CONFIG_SIZE); memset(buf, 0, CONFIG_SIZE);
int sock = socket(AF_INET, SOCK_STREAM, 0); int sock = socket(AF_INET, SOCK_STREAM, 0);
@ -248,7 +252,21 @@ int test_ioctl_SIOCGIFCONF(void) {
int ret_len = conf.ifc_len; int ret_len = conf.ifc_len;
// use a larger buffer when the original one is insufficient
if (ret_len > CONFIG_SIZE) {
free(buf);
char *new_buf = (char *)malloc(ret_len);
if (new_buf == NULL) {
close(sock);
THROW_ERROR("malloc failed");
}
buf = new_buf;
memset(buf, 0, ret_len);
} else {
conf.ifc_len = CONFIG_SIZE; conf.ifc_len = CONFIG_SIZE;
}
conf.ifc_buf = buf; conf.ifc_buf = buf;
if (ioctl(sock, SIOCGIFCONF, &conf) < 0) { if (ioctl(sock, SIOCGIFCONF, &conf) < 0) {
close(sock); close(sock);