mirror of
https://github.com/json-c/json-c.git
synced 2026-04-09 23:39:06 +08:00
Fix memory leak and inconsistent output (due to varying fd numbers) in test_util_file.
This commit is contained in:
@@ -49,6 +49,7 @@ static void test_write_to_file()
|
|||||||
(rv == 0) ? "OK" : "FAIL", outfile2, rv);
|
(rv == 0) ? "OK" : "FAIL", outfile2, rv);
|
||||||
if (rv == 0)
|
if (rv == 0)
|
||||||
stat_and_cat(outfile2);
|
stat_and_cat(outfile2);
|
||||||
|
json_object_put(jso);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void stat_and_cat(const char *file)
|
static void stat_and_cat(const char *file)
|
||||||
@@ -163,11 +164,18 @@ static void test_read_closed()
|
|||||||
int d = open("/dev/null", O_RDONLY, 0);
|
int d = open("/dev/null", O_RDONLY, 0);
|
||||||
if(d < 0)
|
if(d < 0)
|
||||||
{
|
{
|
||||||
puts("FAIL: unable to open");
|
puts("FAIL: unable to open");
|
||||||
|
}
|
||||||
|
// Copy over to a fixed fd number so test output is consistent.
|
||||||
|
int fixed_d = 10;
|
||||||
|
if (dup2(d, fixed_d) < 0)
|
||||||
|
{
|
||||||
|
printf("FAIL: unable to dup to fd %d", fixed_d);
|
||||||
}
|
}
|
||||||
close(d);
|
close(d);
|
||||||
|
close(fixed_d);
|
||||||
|
|
||||||
json_object *jso = json_object_from_fd(d);
|
json_object *jso = json_object_from_fd(fixed_d);
|
||||||
if (jso != NULL)
|
if (jso != NULL)
|
||||||
{
|
{
|
||||||
printf("FAIL: read from closed fd returning non-NULL: 0x%lx\n",
|
printf("FAIL: read from closed fd returning non-NULL: 0x%lx\n",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
OK: json_object_from_fd(./valid.json)={ "foo": 123 }
|
OK: json_object_from_fd(./valid.json)={ "foo": 123 }
|
||||||
OK: json_object_from_file(./not_present.json) correctly returned NULL: json_object_from_file: error opening file ./not_present.json: ERRNO=ENOENT
|
OK: json_object_from_file(./not_present.json) correctly returned NULL: json_object_from_file: error opening file ./not_present.json: ERRNO=ENOENT
|
||||||
|
|
||||||
OK: json_object_from_fd(closed_fd), expecting NULL, EBADF, got:0x0, json_object_from_fd: error reading fd 3: ERRNO=EBADF
|
OK: json_object_from_fd(closed_fd), expecting NULL, EBADF, got:0x0, json_object_from_fd: error reading fd 10: ERRNO=EBADF
|
||||||
|
|
||||||
OK: json_object_to_file(json.out, jso)=0
|
OK: json_object_to_file(json.out, jso)=0
|
||||||
file[json.out], size=336, contents={"foo":1234,"foo1":"abcdefghijklmnopqrstuvwxyz","foo2":"abcdefghijklmnopqrstuvwxyz","foo3":"abcdefghijklmnopqrstuvwxyz","foo4":"abcdefghijklmnopqrstuvwxyz","foo5":"abcdefghijklmnopqrstuvwxyz","foo6":"abcdefghijklmnopqrstuvwxyz","foo7":"abcdefghijklmnopqrstuvwxyz","foo8":"abcdefghijklmnopqrstuvwxyz","foo9":"abcdefghijklmnopqrstuvwxyz"}
|
file[json.out], size=336, contents={"foo":1234,"foo1":"abcdefghijklmnopqrstuvwxyz","foo2":"abcdefghijklmnopqrstuvwxyz","foo3":"abcdefghijklmnopqrstuvwxyz","foo4":"abcdefghijklmnopqrstuvwxyz","foo5":"abcdefghijklmnopqrstuvwxyz","foo6":"abcdefghijklmnopqrstuvwxyz","foo7":"abcdefghijklmnopqrstuvwxyz","foo8":"abcdefghijklmnopqrstuvwxyz","foo9":"abcdefghijklmnopqrstuvwxyz"}
|
||||||
|
|||||||
Reference in New Issue
Block a user