test: Fix memory leaks in fpurge test

Warned-by: gcc -fsanitize=leak
This commit is contained in:
Guillem Jover 2023-03-14 03:05:58 +01:00
parent 5962e03c2d
commit 05a802a2c1

View File

@ -28,12 +28,25 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
static int
test_memstream(FILE *fp, size_t bufsz)
{
fputs("World", fp);
if (fpurge(fp) < 0)
return 1;
fflush(fp);
if (bufsz != 0)
return 1;
return 0;
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
FILE *fp; FILE *fp;
char *buf = NULL; char *buf = NULL;
size_t bufsz = 0; size_t bufsz = 0;
int rc;
if (fpurge(NULL) == 0) if (fpurge(NULL) == 0)
return 1; return 1;
@ -45,13 +58,9 @@ main(int argc, char *argv[])
fclose(fp); fclose(fp);
fp = open_memstream(&buf, &bufsz); fp = open_memstream(&buf, &bufsz);
fputs("World", fp); rc = test_memstream(fp, bufsz);
if (fpurge(fp) < 0) fclose(fp);
return 1;
fflush(fp);
if (bufsz != 0)
return 1;
free(buf); free(buf);
return 0; return rc;
} }