27#define G_LOG_DOMAIN "lib nasl"
84 gchar *keywords[] = {
"ACT_UNKNOWN",
"description",
"NULL",
"SCRIPT_NAME",
85 "COMMAND_LINE",
"_FCT_ANON_ARGS", NULL};
87 for (i = 0; keywords[i] != NULL; i++)
88 *defined_var = g_slist_prepend (*defined_var, keywords[i]);
105 gchar *lala = g_strdup (((
func_info *) lelem)->func_name);
106 return (g_strcmp0 (lala, data));
125 && !g_str_has_suffix (
nasl_name,
".inc"))
133 if ((finfo_aux = g_slist_find_custom (*def_func_tree, fdata->
caller_func,
154 return (g_strcmp0 (lelem, data));
174 && g_strcmp0 (value,
"YES") != 0)
175 *unusedfiles = g_slist_prepend (*unusedfiles, key);
188 if (filename != NULL)
190 nasl_perror (lexic,
"The included file '%s' is never used.",
204 GHashTable **include_files, GHashTable **func_fnames_tab,
205 gchar *err_fname, GSList **called_funcs, GSList **def_func_tree)
209 char *incname = NULL;
210 gchar *tmp_filename = NULL;
212 static gchar *current_fun_def = NULL;
219 g_hash_table_insert (*func_fnames_tab, g_strdup (st->
x.
str_val),
220 g_strdup (err_fname));
231 *def_func_tree = g_slist_prepend (*def_func_tree, finfo);
236 GSList *func_params = NULL;
239 for (; args != NULL; args = args->
link[1])
244 if (!g_slist_find_custom (func_params, args->
x.
str_val,
247 g_slist_prepend (func_params, args->
x.
str_val);
250 g_message (
"%s: Error at or near line %d. "
251 "Parameter \"%s\" passed to function \"%s\" "
252 "was provided multiple times.",
255 g_slist_free (func_params);
260 g_slist_free (func_params);
275 if (!g_slist_find_custom (*called_funcs, st->
x.
str_val,
283 current_fun_def = g_strdup (st->
x.
str_val);
285 g_hash_table_replace (*include_files, incname, g_strdup (
"NO"));
287 err_fname = g_strdup (incname);
291 for (i = 0; i < 4; i++)
294 include_files, func_fnames_tab, err_fname,
295 called_funcs, def_func_tree))
303 g_free (tmp_filename);
326 for (cp = st; cp != NULL; cp = cp->
link[1])
349 if (((value == NULL) && (csv == NULL)) ||
name == NULL)
352 "script_xref() syntax error - should be"
353 " script_xref(name:<name>, value:<value>) or"
354 " script_xref(name:<name>, value:<value>, csv:<CSVs>) or"
355 " script_xref(name:<name>, csv:<CSVs>)\n");
364 if ((value == NULL) && (csv == NULL))
366 nasl_perror (lexic,
" <value> and <csv> is empty)\n");
391 if (!g_strcmp0 (st->
x.
str_val,
"script_xref"))
406 GSList *def_func_tree)
409 GSList *current = def_func_tree;
416 element = current->data;
417 if (g_strcmp0 (element->
func_name, funcname) == 0
418 && g_strcmp0 (element->
caller_file, filename) != 0)
420 current = current->next;
422 while (current != NULL && current->next != NULL);
439 GHashTable **func_fnames_tab, gchar *err_fname,
440 GSList **called_funcs, GSList **def_func_tree)
445 char *incname = NULL;
446 int f_inc_ord, c_inc_order, rc = 0;
447 static int defined_flag = 0;
454 if (!g_slist_find_custom (*called_funcs, st->
x.
str_val,
465 if (st->
x.
str_val != NULL && defined_flag == 1)
477 incname = g_hash_table_lookup (*func_fnames_tab, st->
x.
str_val);
482 GSList *called_f_aux;
483 called_f_aux = g_slist_find_custom (*def_func_tree, st->
x.
str_val,
485 if (called_f_aux != NULL)
509 if (c_inc_order > 0 && c_inc_order < f_inc_ord)
512 lexic,
"%s must be included after %s (usage of %s).",
522 if (*include_files && st->
x.
str_val)
524 if (g_hash_table_lookup (*include_files,
530 g_hash_table_replace (*include_files, incname,
535 if (g_strcmp0 (st->
x.
str_val,
"defined_func") == 0)
540 for (i = 0; i < 4; i++)
543 func_fnames_tab, err_fname, called_funcs,
547 return rc == 0 ? ret : NULL;
558 GHashTable **func_fnames_tab, gchar *err_fname,
559 GSList **defined_var, GSList **called_funcs)
563 static int defined_fn_mode = 0;
564 static int defined_var_mode = 0;
565 static int def_glob_var = 0;
566 static GSList *local_var_list = NULL;
573 if (!g_slist_find_custom (*called_funcs, st->
x.
str_val,
580 if ((defined_fn_mode == 1 || def_glob_var) && st->
type !=
NODE_DECL)
589 && defined_var_mode == 0)
590 defined_var_mode = 1;
593 && defined_fn_mode == 0)
596 defined_var_mode = 0;
606 && (defined_var_mode == 1 || defined_fn_mode == 1))
610 if (!g_slist_find_custom (local_var_list, st->
x.
str_val,
612 *defined_var = g_slist_prepend (*defined_var, st->
x.
str_val);
613 defined_var_mode = 0;
621 if (defined_fn_mode == 1)
623 local_var_list = g_slist_prepend (local_var_list, st->
x.
str_val);
625 if (def_glob_var == 1)
627 *defined_var = g_slist_prepend (*defined_var, st->
x.
str_val);
636 if (st->
x.
str_val != NULL && local_var_list != NULL)
638 local_var_list = g_slist_prepend (local_var_list, st->
x.
str_val);
642 *defined_var = g_slist_prepend (*defined_var, st->
x.
str_val);
650 && defined_var_mode == 0)
652 if (!g_slist_find_custom (*defined_var, st->
x.
str_val,
654 && !g_slist_find_custom (local_var_list, st->
x.
str_val,
658 nasl_perror (lexic,
"The variable %s was not declared",
664 for (i = 0; i < 4; i++)
667 func_fnames_tab, err_fname, defined_var,
677 g_slist_free (local_var_list);
678 local_var_list = NULL;
700 g_slist_prepend (*called_funcs, g_strdup (st->
x.
str_val));
705 for (i = 0; i < 4; i++)
726 if (g_strrstr (st->
x.
str_val,
", ") != NULL)
728 g_message (
"%s: An error in script_xrefs function was found. "
729 "Spaces after a comma are not allow in xrefs names "
736 for (i = 0; i < 4; i++)
755 if (!g_strcmp0 (st->
x.
str_val,
"script_xref"))
759 for (i = 0; i < 4; i++)
781 for (i = 0; i < 4; i++)
784 st_aux = st->
link[i];
786 && !g_strcmp0 (st_aux->
x.
str_val,
"description"))
791 for (i = 0; i < 4; i++)
816 GHashTable *include_files = NULL;
817 GHashTable *func_fnames_tab = NULL;
818 GSList *unusedfiles = NULL;
819 GSList *called_funcs = NULL;
820 GSList *def_func_tree = NULL;
821 gchar *err_fname = NULL;
827 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
829 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
833 lexic_aux->
oid = lexic->
oid;
837 if (desc_block != NULL && desc_block !=
FAKE_CELL)
849 if (
nasl_lint_def (lexic_aux, st, lint_mode, &include_files, &func_fnames_tab,
850 err_fname, &called_funcs, &def_func_tree)
857 if (
nasl_lint_call (lexic_aux, st, &include_files, &func_fnames_tab,
858 err_fname, &called_funcs, &def_func_tree)
867 if (unusedfiles != NULL)
869 if ((g_slist_length (unusedfiles)) > 0)
876 if (
nasl_lint_def (lexic, st, lint_mode, &include_files, &func_fnames_tab,
877 err_fname, &called_funcs, &def_func_tree)
884 GSList *defined_var = NULL;
887 err_fname, &defined_var, &called_funcs);
888 g_slist_free (defined_var);
891 g_slist_free (called_funcs);
893 g_slist_free_full (def_func_tree, (GDestroyNotify)
free_list_func);
894 def_func_tree = NULL;
895 g_hash_table_destroy (include_files);
896 include_files = NULL;
897 g_hash_table_destroy (func_fnames_tab);
898 func_fnames_tab = NULL;
900 g_slist_free (unusedfiles);
static tree_cell * check_description_block_xref(lex_ctxt *lexic, tree_cell *st)
Sanity check of the script_xref parameters in the description block.
static tree_cell * nasl_lint_def(lex_ctxt *lexic, tree_cell *st, int lint_mode, GHashTable **include_files, GHashTable **func_fnames_tab, gchar *err_fname, GSList **called_funcs, GSList **def_func_tree)
Loads all defined functions. Also, It constructs a tree of called functions to help recognize a not d...
static void inc_errors_cnt()
static gint reverse_search(GSList **def_func_tree, GSList *finfo)
Check if an undefined called function is needed or not. This is the case in which the function is cal...
static void init_errors_cnt()
static int get_errors_cnt()
static void print_uncall_files(gpointer filename, gpointer lexic)
It shows a msg for unused included files.
static tree_cell * validate_function(lex_ctxt *lexic, tree_cell *st)
Validate functions.
static void free_list_func(func_info *data)
Free a func_info structure.
static gint list_cmp(gconstpointer lelem, gconstpointer data)
This function is called by g_slist_find_custom.
static int is_deffunc_used(const char *funcname, const char *filename, GSList *def_func_tree)
Returns 1 if the function is at least used once by another caller than filename otherwise 0.
static void add_predef_varname(GSList **defined_var)
Add keywords to the varnames list.
static tree_cell * check_description_block(lex_ctxt *lexic, tree_cell *st)
Sanity check of the description block.
static tree_cell * validate_script_xref(lex_ctxt *lexic, tree_cell *st)
Validates parameters of a script_xref function call.
static tree_cell * nasl_lint_call(lex_ctxt *lexic, tree_cell *st, GHashTable **include_files, GHashTable **func_fnames_tab, gchar *err_fname, GSList **called_funcs, GSList **def_func_tree)
Check if a called function was defined.
tree_cell * nasl_lint(lex_ctxt *lexic, tree_cell *st)
Search for errors in a nasl script.
static void check_called_files(gpointer key, gpointer value, GSList **unusedfiles)
This function is called by g_hash_table_foreach to check if an include file was used or not....
struct st_func_info func_info
Define struct to store information about a called function.
static tree_cell * find_description_block(lex_ctxt *lexic, tree_cell *st)
Sanity check of the description block.
static char * get_argument_by_name(tree_cell *st, char *name)
Checks if a given Arguments is within a given Argument List.
static tree_cell * make_call_func_list(lex_ctxt *lexic, tree_cell *st, GSList **called_funcs)
Make a list of all called functions.
static gint list_cmp1(gconstpointer lelem, gconstpointer data)
This function is called by g_slist_find_custom.
static tree_cell * nasl_lint_defvar(lex_ctxt *lexic, tree_cell *st, GHashTable **include_files, GHashTable **func_fnames_tab, gchar *err_fname, GSList **defined_var, GSList **called_funcs)
Consider all cases in which a variable is set, and add it to a list. If a variable is read,...
void nasl_set_filename(const char *filename)
void nasl_perror(lex_ctxt *lexic, char *msg,...)
const char * nasl_get_filename(const char *function)
int nasl_get_include_order(const char *)
tree_cell * decl_nasl_func(lex_ctxt *lexic, tree_cell *decl_node, int lint_mode)
nasl_func * get_func_ref_by_name(lex_ctxt *ctxt, const char *name)
struct st_nasl_func nasl_func
nasl_func * func_is_internal(const char *)
void add_nasl_library(GSList **list)
Add "built-in" variables to a list.
void free_lex_ctxt(lex_ctxt *c)
lex_ctxt * init_empty_lex_ctxt()
struct struct_lex_ctxt lex_ctxt
tree_cell * alloc_typed_cell(int typ)
union TC::@332262321161220155002104006201360276211317150140 x
Define struct to store information about a called function.
struct script_infos * script_infos