aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2020-03-16 19:38:42 +0100
committerChristian Grothoff <christian@grothoff.org>2020-03-16 19:38:42 +0100
commit39c6d488d4b8b8061ac33a902064cd5b2167b60c (patch)
treec4a46545191b870c20f6054308c3d43f4ee1da3b
parent16864fd3b848f78fa6e1928c7ae6b37826c316d3 (diff)
downloadgnunet-39c6d488d4b8b8061ac33a902064cd5b2167b60c.tar.gz
gnunet-39c6d488d4b8b8061ac33a902064cd5b2167b60c.zip
better error handling
-rw-r--r--src/json/json_generator.c114
1 files changed, 94 insertions, 20 deletions
diff --git a/src/json/json_generator.c b/src/json/json_generator.c
index 89fd53265..9b2fb7fbb 100644
--- a/src/json/json_generator.c
+++ b/src/json/json_generator.c
@@ -45,6 +45,7 @@ GNUNET_JSON_from_data (const void *data,
45 buf = GNUNET_STRINGS_data_to_string_alloc (data, size); 45 buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
46 json = json_string (buf); 46 json = json_string (buf);
47 GNUNET_free (buf); 47 GNUNET_free (buf);
48 GNUNET_break (NULL != json);
48 return json; 49 return json;
49} 50}
50 51
@@ -64,17 +65,34 @@ GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
64 GNUNET_TIME_round_abs (&stamp)); 65 GNUNET_TIME_round_abs (&stamp));
65 66
66 j = json_object (); 67 j = json_object ();
67 68 if (NULL == j)
69 {
70 GNUNET_break (0);
71 return NULL;
72 }
68 if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) 73 if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
69 { 74 {
70 json_object_set_new (j, 75 if (0 !=
71 "t_ms", 76 json_object_set_new (j,
72 json_string ("never")); 77 "t_ms",
78 json_string ("never")))
79 {
80 GNUNET_break (0);
81 json_decref (j);
82 return NULL;
83 }
73 return j; 84 return j;
74 } 85 }
75 json_object_set_new (j, 86 if (0 !=
76 "t_ms", 87 json_object_set_new (j,
77 json_integer ((json_int_t) (stamp.abs_value_us / 1000LL))); 88 "t_ms",
89 json_integer ((json_int_t) (stamp.abs_value_us
90 / 1000LL))))
91 {
92 GNUNET_break (0);
93 json_decref (j);
94 return NULL;
95 }
78 return j; 96 return j;
79} 97}
80 98
@@ -107,17 +125,34 @@ GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
107 GNUNET_TIME_round_rel (&stamp)); 125 GNUNET_TIME_round_rel (&stamp));
108 126
109 j = json_object (); 127 j = json_object ();
110 128 if (NULL == j)
129 {
130 GNUNET_break (0);
131 return NULL;
132 }
111 if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 133 if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
112 { 134 {
113 json_object_set_new (j, 135 if (0 !=
114 "d_ms", 136 json_object_set_new (j,
115 json_string ("forever")); 137 "d_ms",
138 json_string ("forever")))
139 {
140 GNUNET_break (0);
141 json_decref (j);
142 return NULL;
143 }
116 return j; 144 return j;
117 } 145 }
118 json_object_set_new (j, 146 if (0 !=
119 "d_ms", 147 json_object_set_new (j,
120 json_integer ((json_int_t) (stamp.rel_value_us / 1000LL))); 148 "d_ms",
149 json_integer ((json_int_t) (stamp.rel_value_us
150 / 1000LL))))
151 {
152 GNUNET_break (0);
153 json_decref (j);
154 return NULL;
155 }
121 return j; 156 return j;
122} 157}
123 158
@@ -187,10 +222,27 @@ GNUNET_JSON_from_gnsrecord (const char*rname,
187 json_t *records; 222 json_t *records;
188 223
189 data = json_object (); 224 data = json_object ();
190 json_object_set_new (data, 225 if (NULL == data)
191 "record_name", 226 {
192 json_string (rname)); 227 GNUNET_break (0);
228 return NULL;
229 }
230 if (0 !=
231 json_object_set_new (data,
232 "record_name",
233 json_string (rname)))
234 {
235 GNUNET_break (0);
236 json_decref (data);
237 return NULL;
238 }
193 records = json_array (); 239 records = json_array ();
240 if (NULL == records)
241 {
242 GNUNET_break (0);
243 json_decref (data);
244 return NULL;
245 }
194 for (int i = 0; i < rd_count; i++) 246 for (int i = 0; i < rd_count; i++)
195 { 247 {
196 value_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, 248 value_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
@@ -212,11 +264,33 @@ GNUNET_JSON_from_gnsrecord (const char*rname,
212 expiration_time_str, 264 expiration_time_str,
213 "flag", 265 "flag",
214 rd[i].flags); 266 rd[i].flags);
215 GNUNET_assert (NULL != record);
216 GNUNET_free (value_str); 267 GNUNET_free (value_str);
217 json_array_append_new (records, record); 268 if (NULL == record)
269 {
270 GNUNET_break (0);
271 json_decref (records);
272 json_decref (data);
273 return NULL;
274 }
275 if (0 !=
276 json_array_append_new (records,
277 record))
278 {
279 GNUNET_break (0);
280 json_decref (records);
281 json_decref (data);
282 return NULL;
283 }
284 }
285 if (0 !=
286 json_object_set_new (data,
287 "data",
288 records))
289 {
290 GNUNET_break (0);
291 json_decref (data);
292 return NULL;
218 } 293 }
219 json_object_set_new (data, "data", records);
220 return data; 294 return data;
221} 295}
222 296