OpenVAS Scanner  22.7.9
lint.h File Reference
#include "nasl_lex_ctxt.h"
Include dependency graph for lint.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  nasl_lint_feature_flags { NLFF_NONE = 0, NLFF_STRICT_INCLUDES = 1 }
 

Functions

void nasl_lint_feature_flags (int flags)
 
tree_cellnasl_lint (lex_ctxt *lexic, tree_cell *st)
 Search for errors in a nasl script. More...
 

Enumeration Type Documentation

◆ nasl_lint_feature_flags

Enumerator
NLFF_NONE 
NLFF_STRICT_INCLUDES 

Definition at line 13 of file lint.h.

14 {
15  NLFF_NONE = 0,
17 };

Function Documentation

◆ nasl_lint()

tree_cell* nasl_lint ( lex_ctxt lexic,
tree_cell st 
)

Search for errors in a nasl script.

Parameters
[in]lexicnasl context.
[in]ststructure tree of a nasl script.
Returns
FAKE_CELL if no error was found, otherwise NULL or tree_cell which has number of errors as x.i_val.

Definition at line 811 of file lint.c.

812 {
813  lex_ctxt *lexic_aux;
814  tree_cell *ret = FAKE_CELL;
815  int lint_mode = 1;
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;
822  tree_cell *desc_block = FAKE_CELL;
823  init_errors_cnt ();
824 
825  nasl_name = g_strdup (nasl_get_filename (st->x.str_val));
826  include_files =
827  g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
828  func_fnames_tab =
829  g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
830 
831  lexic_aux = init_empty_lex_ctxt ();
832  lexic_aux->script_infos = lexic->script_infos;
833  lexic_aux->oid = lexic->oid;
834 
835  /* Check description block sanity. */
836  desc_block = find_description_block (lexic_aux, st);
837  if (desc_block != NULL && desc_block != FAKE_CELL)
838  {
839  /* FAKE_CELL if success, NULL otherwise which counts as error */
840  if (check_description_block (lexic_aux, desc_block) == NULL)
841  {
842  inc_errors_cnt ();
843  }
844  }
845  /* Make a list of all called functions */
846  make_call_func_list (lexic_aux, st, &called_funcs);
847 
848  /* Loads all defined functions. */
849  if (nasl_lint_def (lexic_aux, st, lint_mode, &include_files, &func_fnames_tab,
850  err_fname, &called_funcs, &def_func_tree)
851  == NULL)
852  {
853  inc_errors_cnt ();
854  }
855  /* Check if a called function was defined. */
856 
857  if (nasl_lint_call (lexic_aux, st, &include_files, &func_fnames_tab,
858  err_fname, &called_funcs, &def_func_tree)
859  == NULL)
860  {
861  inc_errors_cnt ();
862  }
863 
864  /* Check if the included files are used or not. */
865  g_hash_table_foreach (include_files, (GHFunc) check_called_files,
866  &unusedfiles);
867  if (unusedfiles != NULL)
868  g_slist_foreach (unusedfiles, (GFunc) print_uncall_files, lexic_aux);
869  if ((g_slist_length (unusedfiles)) > 0)
870  {
871  inc_errors_cnt ();
872  }
873 
874  /* Now check that each function was loaded just once. */
875  lint_mode = 0;
876  if (nasl_lint_def (lexic, st, lint_mode, &include_files, &func_fnames_tab,
877  err_fname, &called_funcs, &def_func_tree)
878  == NULL)
879  {
880  inc_errors_cnt ();
881  }
882 
883  /* Check if a variable was declared. */
884  GSList *defined_var = NULL;
885  add_predef_varname (&defined_var);
886  ret = nasl_lint_defvar (lexic_aux, st, &include_files, &func_fnames_tab,
887  err_fname, &defined_var, &called_funcs);
888  g_slist_free (defined_var);
889  defined_var = NULL;
890 
891  g_slist_free (called_funcs);
892  called_funcs = NULL;
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;
899  g_free (err_fname);
900  g_slist_free (unusedfiles);
901  unusedfiles = NULL;
902  free_lex_ctxt (lexic_aux);
903 
904  if (get_errors_cnt () > 0)
905  {
906  ret = alloc_typed_cell (NODE_VAR);
907  ret->x.i_val = get_errors_cnt ();
908  }
909 
910  return ret;
911 }

References add_predef_varname(), alloc_typed_cell(), check_called_files(), check_description_block(), FAKE_CELL, find_description_block(), free_lex_ctxt(), free_list_func(), get_errors_cnt(), TC::i_val, inc_errors_cnt(), init_empty_lex_ctxt(), init_errors_cnt(), make_call_func_list(), nasl_get_filename(), nasl_lint_call(), nasl_lint_def(), nasl_lint_defvar(), nasl_name, NODE_VAR, struct_lex_ctxt::oid, print_uncall_files(), struct_lex_ctxt::script_infos, TC::str_val, and TC::x.

Referenced by exec_nasl_script().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_lint_feature_flags()

void nasl_lint_feature_flags ( int  flags)

Definition at line 429 of file lint.c.

430 {
431  features = flag;
432 }

References features.

free_list_func
static void free_list_func(func_info *data)
Free a func_info structure.
Definition: lint.c:66
TC::str_val
char * str_val
Definition: nasl_tree.h:103
TC::x
union TC::@5 x
FAKE_CELL
#define FAKE_CELL
Definition: nasl_tree.h:110
nasl_lint_def
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...
Definition: lint.c:203
make_call_func_list
static tree_cell * make_call_func_list(lex_ctxt *lexic, tree_cell *st, GSList **called_funcs)
Make a list of all called functions.
Definition: lint.c:687
add_predef_varname
static void add_predef_varname(GSList **defined_var)
Add keywords to the varnames list.
Definition: lint.c:81
init_errors_cnt
static void init_errors_cnt()
Definition: lint.c:43
NODE_VAR
@ NODE_VAR
Definition: nasl_tree.h:31
print_uncall_files
static void print_uncall_files(gpointer filename, gpointer lexic)
It shows a msg for unused included files.
Definition: lint.c:186
get_errors_cnt
static int get_errors_cnt()
Definition: lint.c:54
NLFF_NONE
@ NLFF_NONE
Definition: lint.h:15
struct_lex_ctxt::oid
const char * oid
Definition: nasl_lex_ctxt.h:31
free_lex_ctxt
void free_lex_ctxt(lex_ctxt *c)
Definition: nasl_lex_ctxt.c:43
struct_lex_ctxt::script_infos
struct script_infos * script_infos
Definition: nasl_lex_ctxt.h:30
NLFF_STRICT_INCLUDES
@ NLFF_STRICT_INCLUDES
Definition: lint.h:16
TC
Definition: nasl_tree.h:94
struct_lex_ctxt
Definition: nasl_lex_ctxt.h:23
find_description_block
static tree_cell * find_description_block(lex_ctxt *lexic, tree_cell *st)
Sanity check of the description block.
Definition: lint.c:773
inc_errors_cnt
static void inc_errors_cnt()
Definition: lint.c:48
nasl_lint_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.
Definition: lint.c:438
nasl_get_filename
const char * nasl_get_filename(const char *function)
Definition: nasl_debug.c:60
features
int features
Definition: lint.c:426
alloc_typed_cell
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:28
check_called_files
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....
Definition: lint.c:169
nasl_lint_defvar
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,...
Definition: lint.c:557
init_empty_lex_ctxt
lex_ctxt * init_empty_lex_ctxt()
Definition: nasl_lex_ctxt.c:20
check_description_block
static tree_cell * check_description_block(lex_ctxt *lexic, tree_cell *st)
Sanity check of the description block.
Definition: lint.c:749
nasl_name
char * nasl_name
Definition: lint.c:39
TC::i_val
long int i_val
Definition: nasl_tree.h:104