json_load_file: Initialize the error struct properly
Failing to do this has the effect that the error message is not returned when the input file cannot be opened (e.g. if it doesn't exist). Thanks to Martin Vopatek for reporting.
This commit is contained in:
parent
15d992cb6a
commit
f243930b68
@ -864,6 +864,8 @@ json_t *json_load_file(const char *path, json_error_t *error)
|
|||||||
json_t *result;
|
json_t *result;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
|
||||||
|
error_init(error);
|
||||||
|
|
||||||
fp = fopen(path, "r");
|
fp = fopen(path, "r");
|
||||||
if(!fp)
|
if(!fp)
|
||||||
{
|
{
|
||||||
|
1
test/.gitignore
vendored
1
test/.gitignore
vendored
@ -3,6 +3,7 @@ loads_dumps
|
|||||||
load_file_dump_file
|
load_file_dump_file
|
||||||
testlogs
|
testlogs
|
||||||
testprogs/test_array
|
testprogs/test_array
|
||||||
|
testprogs/test_load
|
||||||
testprogs/test_number
|
testprogs/test_number
|
||||||
testprogs/test_object
|
testprogs/test_object
|
||||||
testprogs/test_simple
|
testprogs/test_simple
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
check_PROGRAMS = test_array test_simple test_number test_object
|
check_PROGRAMS = test_array test_load test_simple test_number test_object
|
||||||
|
|
||||||
test_array_SOURCES = test_array.c util.h
|
test_array_SOURCES = test_array.c util.h
|
||||||
|
test_load_SOURCES = test_load.c util.h
|
||||||
test_simple_SOURCES = test_simple.c util.h
|
test_simple_SOURCES = test_simple.c util.h
|
||||||
test_number_SOURCES = test_number.c util.h
|
test_number_SOURCES = test_number.c util.h
|
||||||
test_object_SOURCES = test_object.c util.h
|
test_object_SOURCES = test_object.c util.h
|
||||||
|
24
test/testprogs/test_load.c
Normal file
24
test/testprogs/test_load.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
|
||||||
|
*
|
||||||
|
* Jansson is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the MIT license. See LICENSE for details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <jansson.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
json_t *json;
|
||||||
|
json_error_t error;
|
||||||
|
|
||||||
|
json = json_load_file("/path/to/nonexistent/file.json", &error);
|
||||||
|
if(error.line != -1)
|
||||||
|
fail("json_load_file returned an invalid line number");
|
||||||
|
if(strcmp(error.text, "unable to open /path/to/nonexistent/file.json: No such file or directory") != 0)
|
||||||
|
fail("json_load_file returned an invalid error message");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user