7
7
\*******************************************************************/
8
8
9
9
#include < iostream>
10
+ #include < fstream>
10
11
11
12
#include < util/prefix.h>
12
13
#include < util/simplify_expr.h>
14
+ #include < util/json.h>
13
15
14
16
#include < ansi-c/string_constant.h>
15
17
@@ -39,7 +41,7 @@ class taint_analysist:public messaget
39
41
const std::string &taint_file_name,
40
42
goto_functionst &goto_functions,
41
43
bool show_full,
42
- bool json );
44
+ const std::string &json_file_name );
43
45
44
46
protected:
45
47
const namespacet &ns;
@@ -218,10 +220,13 @@ bool taint_analysist::operator()(
218
220
const std::string &taint_file_name,
219
221
goto_functionst &goto_functions,
220
222
bool show_full,
221
- bool json )
223
+ const std::string &json_file_name )
222
224
{
223
225
try
224
226
{
227
+ jsont json_result=jsont::json_array ();
228
+ bool use_json=!json_file_name.empty ();
229
+
225
230
status () << " Reading taint file `" << taint_file_name
226
231
<< " '" << eom;
227
232
@@ -323,18 +328,18 @@ bool taint_analysist::operator()(
323
328
if (first)
324
329
{
325
330
first=false ;
326
- if (!json )
331
+ if (!use_json )
327
332
std::cout << " \n "
328
333
" ******** Function " << symbol.display_name () << ' \n ' ;
329
334
}
330
335
331
- if (json )
336
+ if (use_json )
332
337
{
333
- std::cout << " { \n " ;
334
- std::cout << " \" bug_class\" : \" " << i_it->source_location .get_property_class () << " \" , \n " ;
335
- std::cout << " \" file\" : \" " << i_it->source_location .get_file () << " \" , \n " ;
336
- std::cout << " \" line\" : " << i_it->source_location .get_line () << " \n " ;
337
- std::cout << " } \n " ;
338
+ jsont json= jsont::json_object () ;
339
+ json[ " bug_class" ]= jsont::json_string ( id2string ( i_it->source_location .get_property_class ())) ;
340
+ json[ " file" ]= jsont::json_string ( id2string ( i_it->source_location .get_file ())) ;
341
+ json[ " line" ]= jsont::json_number ( id2string ( i_it->source_location .get_line ())) ;
342
+ json_result. array . push_back (json) ;
338
343
}
339
344
else
340
345
{
@@ -346,7 +351,24 @@ bool taint_analysist::operator()(
346
351
}
347
352
}
348
353
}
354
+
355
+ if (use_json)
356
+ {
357
+ std::ofstream json_out (json_file_name);
349
358
359
+ if (!json_out)
360
+ {
361
+ error () << " Failed to open json output `"
362
+ << json_file_name << " '" << eom;
363
+ return true ;
364
+ }
365
+
366
+ status () << " Analysis result is written to `"
367
+ << json_file_name << " '" << eom;
368
+
369
+ json_out << json_result << ' \n ' ;
370
+ }
371
+
350
372
return false ;
351
373
}
352
374
catch (const char *error_msg)
@@ -383,10 +405,11 @@ bool taint_analysis(
383
405
const std::string &taint_file_name,
384
406
message_handlert &message_handler,
385
407
bool show_full,
386
- bool json )
408
+ const std::string &json_file_name )
387
409
{
388
410
taint_analysist taint_analysis (ns);
389
411
taint_analysis.set_message_handler (message_handler);
390
- return taint_analysis (taint_file_name, goto_functions, show_full, json);
412
+ return taint_analysis (
413
+ taint_file_name, goto_functions, show_full, json_file_name);
391
414
}
392
415
0 commit comments