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

Go to the source code of this file.

Functions

tree_cellnasl_exec (lex_ctxt *, tree_cell *)
 Execute a parse tree.
long int cell_cmp (lex_ctxt *, tree_cell *, tree_cell *)
tree_cellcell2atom (lex_ctxt *, tree_cell *)

Function Documentation

◆ cell2atom()

tree_cell * cell2atom ( lex_ctxt * lexic,
tree_cell * c1 )
Returns
A 'referenced' cell.

Definition at line 194 of file exec.c.

195{
196 tree_cell *c2 = NULL, *ret = NULL;
197 if (c1 == NULL || c1 == FAKE_CELL)
198 return c1;
199
200 switch (c1->type)
201 {
202 case CONST_INT:
203 case CONST_STR:
204 case CONST_DATA:
205 case REF_ARRAY:
206 case DYN_ARRAY:
207 ref_cell (c1);
208 return c1;
209 default:
210 c2 = nasl_exec (lexic, c1);
211 ret = cell2atom (lexic, c2);
212 deref_cell (c2);
213 return ret;
214 }
215}
tree_cell * cell2atom(lex_ctxt *lexic, tree_cell *c1)
Definition exec.c:194
tree_cell * nasl_exec(lex_ctxt *lexic, tree_cell *st)
Execute a parse tree.
Definition exec.c:770
void ref_cell(tree_cell *c)
Definition nasl_tree.c:164
void deref_cell(tree_cell *c)
Definition nasl_tree.c:178
@ CONST_DATA
Definition nasl_tree.h:82
@ DYN_ARRAY
Definition nasl_tree.h:90
@ CONST_STR
Definition nasl_tree.h:80
@ CONST_INT
Definition nasl_tree.h:79
@ REF_ARRAY
Definition nasl_tree.h:89
struct TC tree_cell
#define FAKE_CELL
Definition nasl_tree.h:110
short type
Definition nasl_tree.h:95

◆ cell_cmp()

long int cell_cmp ( lex_ctxt * lexic,
tree_cell * c1,
tree_cell * c2 )

Definition at line 218 of file exec.c.

219{
220 int flag, typ, typ1, typ2;
221 long int x1, x2;
222 char *s1, *s2;
223 int len_s1, len_s2, len_min;
224
225 if (c1 == NULL || c1 == FAKE_CELL)
226 nasl_perror (lexic, "cell_cmp: c1 == NULL !\n");
227 if (c2 == NULL || c2 == FAKE_CELL)
228 nasl_perror (lexic, "cell_cmp: c2 == NULL !\n");
229
230 /* We first convert the cell to atomic types. */
231 c1 = cell2atom (lexic, c1);
232 c2 = cell2atom (lexic, c2);
233
234 /*
235 * Comparing anything to something else which is entirely different
236 * may lead to unpredictable results.
237 * Here are the rules:
238 * 1. No problem with same types, although we do not compare arrays yet
239 * 2. No problem with CONST_DATA / CONST_STR
240 * 3. When an integer is compared to a string, the integer is converted
241 * 4. When NULL is compared to an integer, it is converted to 0
242 * 5. When NULL is compared to a string, it is converted to ""
243 * 6. NULL is "smaller" than anything else (i.e. an array)
244 * Anything else is an error
245 */
246 typ1 = cell_type (c1);
247 typ2 = cell_type (c2);
248
249 if (typ1 == 0 && typ2 == 0) /* Two NULL */
250 {
251 deref_cell (c1);
252 deref_cell (c2);
253 return 0;
254 }
255
256 if (typ1 == typ2) /* Same type, no problem */
257 typ = typ1;
258 else if ((typ1 == CONST_DATA || typ1 == CONST_STR)
259 && (typ2 == CONST_DATA || typ2 == CONST_STR))
260 typ = CONST_DATA; /* Same type in fact (string) */
261 /* We convert an integer into a string before compare */
262 else if ((typ1 == CONST_INT && (typ2 == CONST_DATA || typ2 == CONST_STR))
263 || (typ2 == CONST_INT && (typ1 == CONST_DATA || typ1 == CONST_STR)))
264 typ = CONST_DATA;
265 else if (typ1 == 0) /* 1st argument is null */
266 if (typ2 == CONST_INT || typ2 == CONST_DATA || typ2 == CONST_STR)
267 typ = typ2; /* We convert it to 0 or "" */
268 else
269 {
270 deref_cell (c1);
271 deref_cell (c2);
272 return -1; /* NULL is smaller than anything else */
273 }
274 else if (typ2 == 0) /* 2nd argument is null */
275 if (typ1 == CONST_INT || typ1 == CONST_DATA || typ1 == CONST_STR)
276 typ = typ1; /* We convert it to 0 or "" */
277 else
278 {
279 deref_cell (c1);
280 deref_cell (c2);
281 return 1; /* Anything else is greater than NULL */
282 }
283 else
284 {
285 gchar *n1, *n2;
286
287 n1 = cell2str (lexic, c1);
288 n2 = cell2str (lexic, c2);
289 nasl_perror (lexic,
290 "cell_cmp: comparing '%s' of type %s and '%s' of "
291 "type %s does not make sense\n",
292 n1, nasl_type_name (typ1), n2, nasl_type_name (typ2));
293 g_free (n1);
294 g_free (n2);
295 deref_cell (c1);
296 deref_cell (c2);
297 return 0;
298 }
299
300 switch (typ)
301 {
302 case CONST_INT:
303 x1 = cell2int (lexic, c1);
304 x2 = cell2int (lexic, c2);
305 deref_cell (c1);
306 deref_cell (c2);
307 return x1 - x2;
308
309 case CONST_STR:
310 case CONST_DATA:
311 s1 = cell2str (lexic, c1);
312 if (typ1 == CONST_STR || typ1 == CONST_DATA)
313 len_s1 = c1->size;
314 else if (s1 == NULL)
315 len_s1 = 0;
316 else
317 len_s1 = strlen (s1);
318
319 s2 = cell2str (lexic, c2);
320 if (typ2 == CONST_STR || typ2 == CONST_DATA)
321 len_s2 = c2->size;
322 else if (s2 == NULL)
323 len_s2 = 0;
324 else
325 len_s2 = strlen (s2);
326
327 len_min = len_s1 < len_s2 ? len_s1 : len_s2;
328 flag = 0;
329
330 if (len_min > 0)
331 flag = memcmp (s1, s2, len_min);
332 if (flag == 0)
333 flag = len_s1 - len_s2;
334
335 g_free (s1);
336 g_free (s2);
337 deref_cell (c1);
338 deref_cell (c2);
339 return flag;
340
341 case REF_ARRAY:
342 case DYN_ARRAY:
343 g_message ("cell_cmp: cannot compare arrays yet");
344 deref_cell (c1);
345 deref_cell (c2);
346 return 0;
347
348 default:
349 g_message ("cell_cmp: don't known how to compare %s and %s",
350 nasl_type_name (typ1), nasl_type_name (typ2));
351 deref_cell (c1);
352 deref_cell (c2);
353 return 0;
354 }
355}
static long int cell2int(lex_ctxt *lexic, tree_cell *c)
Definition exec.c:124
static char * cell2str(lex_ctxt *lexic, tree_cell *c)
Definition exec.c:150
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition nasl_debug.c:105
int cell_type(const tree_cell *c)
Definition nasl_tree.c:404
const char * nasl_type_name(int t)
Definition nasl_tree.c:343
long int size
Definition nasl_tree.h:99

References cell2atom(), cell2int(), cell2str(), cell_type(), CONST_DATA, CONST_INT, CONST_STR, deref_cell(), DYN_ARRAY, FAKE_CELL, nasl_perror(), nasl_type_name(), REF_ARRAY, and TC::size.

Referenced by nasl_exec(), and var_cmp().

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

◆ nasl_exec()

tree_cell * nasl_exec ( lex_ctxt * lexic,
tree_cell * st )

Execute a parse tree.

Todo
There is a lot of duplicated code in following cases, could be refactored.

Definition at line 770 of file exec.c.

771{
772 tree_cell *ret = NULL, *ret2 = NULL, *tc1 = NULL, *tc2 = NULL, *tc3 = NULL,
773 *idx = NULL, *args;
774 int flag, z;
775 char *s1 = NULL, *s2 = NULL, *s3 = NULL, *p = NULL;
776 char *p1, *p2;
777 int len1, len2;
778 nasl_func *pf = NULL;
779 long int x, y, n;
780 int i, lint_mode = 0;
781
782 if (st)
783 if (st->line_nb != 0)
784 lexic->line_nb = st->line_nb;
785 /* return */
786 if (lexic->ret_val != NULL)
787 {
788 ref_cell (lexic->ret_val);
789 return lexic->ret_val;
790 }
791
792 /* break or continue */
793 if (lexic->break_flag || lexic->cont_flag)
794 return FAKE_CELL;
795
796 if (st == FAKE_CELL)
797 return FAKE_CELL;
798
799 if (st == NULL)
800 {
801 return NULL;
802 }
803
804 if (nasl_trace_fp != NULL)
806
807 switch (st->type)
808 {
809 case NODE_IF_ELSE:
810 ret = nasl_exec (lexic, st->link[0]);
811#ifdef STOP_AT_FIRST_ERROR
812 if (ret == NULL)
813 return NULL;
814#endif
815 if (cell2bool (lexic, ret))
816 ret2 = nasl_exec (lexic, st->link[1]);
817 else if (st->link[2] != NULL) /* else branch */
818 ret2 = nasl_exec (lexic, st->link[2]);
819 else /* No else */
820 ret2 = FAKE_CELL;
821 deref_cell (ret);
822 return ret2;
823
824 case NODE_INSTR_L: /* Block. [0] = first instr, [1] = tail */
825 ret = nasl_exec (lexic, st->link[0]);
826 if (st->link[1] == NULL || lexic->break_flag || lexic->cont_flag)
827 return ret;
828 deref_cell (ret);
829 ret = nasl_exec (lexic, st->link[1]);
830 return ret;
831
832 case NODE_FOR:
833 /* [0] = start expr, [1] = cond, [2] = end_expr, [3] = block */
834 ret2 = nasl_exec (lexic, st->link[0]);
835#ifdef STOP_AT_FIRST_ERROR
836 if (ret2 == NULL)
837 return NULL;
838#endif
839 deref_cell (ret2);
840 for (;;)
841 {
842 /* Break the loop if 'return' */
843 if (lexic->ret_val != NULL)
844 {
845 ref_cell (lexic->ret_val);
846 return lexic->ret_val;
847 }
848
849 /* condition */
850 if ((ret = nasl_exec (lexic, st->link[1])) == NULL)
851 return NULL; /* We can return here, as NULL is false */
852 flag = cell2bool (lexic, ret);
853 deref_cell (ret);
854 if (!flag)
855 break;
856 /* block */
857 ret = nasl_exec (lexic, st->link[3]);
858#ifdef STOP_AT_FIRST_ERROR
859 if (ret == NULL)
860 return NULL;
861#endif
862 deref_cell (ret);
863
864 /* break */
865 if (lexic->break_flag)
866 {
867 lexic->break_flag = 0;
868 return FAKE_CELL;
869 }
870
871 lexic->cont_flag = 0; /* No need to test if set */
872
873 /* end expression */
874 ret = nasl_exec (lexic, st->link[2]);
875#ifdef STOP_AT_FIRST_ERROR
876 if (ret == NULL)
877 return NULL;
878#endif
879 deref_cell (ret);
880 }
881 return FAKE_CELL;
882
883 case NODE_WHILE:
884 /* [0] = cond, [1] = block */
885 for (;;)
886 {
887 /* return? */
888 if (lexic->ret_val != NULL)
889 {
890 ref_cell (lexic->ret_val);
891 return lexic->ret_val;
892 }
893 /* Condition */
894 if ((ret = nasl_exec (lexic, st->link[0])) == NULL)
895 return NULL; /* NULL is false */
896 flag = cell2bool (lexic, ret);
897 deref_cell (ret);
898 if (!flag)
899 break;
900 /* Block */
901 ret = nasl_exec (lexic, st->link[1]);
902#ifdef STOP_AT_FIRST_ERROR
903 if (ret == NULL)
904 return NULL;
905#endif
906 deref_cell (ret);
907
908 /* break */
909 if (lexic->break_flag)
910 {
911 lexic->break_flag = 0;
912 return FAKE_CELL;
913 }
914 lexic->cont_flag = 0;
915 }
916 return FAKE_CELL;
917
919 /* [0] = block, [1] = cond */
920 for (;;)
921 {
922 /* return? */
923 if (lexic->ret_val != NULL)
924 {
925 ref_cell (lexic->ret_val);
926 return lexic->ret_val;
927 }
928 /* Block */
929 ret = nasl_exec (lexic, st->link[0]);
930#ifdef STOP_AT_FIRST_ERROR
931 if (ret == NULL)
932 return NULL;
933#endif
934 deref_cell (ret);
935
936 /* break */
937 if (lexic->break_flag)
938 {
939 lexic->break_flag = 0;
940 return FAKE_CELL;
941 }
942 lexic->cont_flag = 0;
943
944 /* Condition */
945 ret = nasl_exec (lexic, st->link[1]);
946#ifdef STOP_AT_FIRST_ERROR
947 if (ret == NULL)
948 return NULL;
949#endif
950 flag = cell2bool (lexic, ret);
951 deref_cell (ret);
952 if (flag)
953 break;
954 }
955 return FAKE_CELL;
956
957 case NODE_FOREACH:
958 /* str_val = index name, [0] = array, [1] = block */
959 {
960 nasl_iterator ai;
961 tree_cell *v, *a, *val;
962
963 v = get_variable_by_name (lexic, st->x.str_val);
964 if (v == NULL)
965 return NULL; /* We cannot go on if we have no variable to iterate */
966 a = nasl_exec (lexic, st->link[0]);
967 ai = nasl_array_iterator (lexic, a);
968 while ((val = nasl_iterate_array (&ai)) != NULL)
969 {
970 tc1 = nasl_affect (v, val);
971 ret = nasl_exec (lexic, st->link[1]);
972 deref_cell (val);
973 deref_cell (tc1);
974#ifdef STOP_AT_FIRST_ERROR
975 if (ret == NULL)
976 break;
977#endif
978 deref_cell (ret);
979
980 /* return */
981 if (lexic->ret_val != NULL)
982 break;
983 /* break */
984 if (lexic->break_flag)
985 {
986 lexic->break_flag = 0;
987 break;
988 }
989 lexic->cont_flag = 0;
990 }
991 free_array (ai.a);
992 g_free (ai.a);
993 deref_cell (a);
994 deref_cell (v);
995 }
996 return FAKE_CELL;
997
998 case NODE_FUN_DEF:
999 /* x.str_val = function name, [0] = argdecl, [1] = block */
1000 /* 3rd arg is only for lint. Hier is always 0 */
1001 ret = decl_nasl_func (lexic, st, lint_mode);
1002 return ret;
1003
1004 case NODE_FUN_CALL:
1005 pf = get_func_ref_by_name (lexic, st->x.str_val);
1006 if (pf == NULL)
1007 {
1008 nasl_perror (lexic, "Undefined function '%s'\n", st->x.str_val);
1009 return NULL;
1010 }
1011 args = st->link[0];
1012 ret = nasl_func_call (lexic, pf, args);
1013 return ret;
1014
1015 case NODE_REPEATED:
1016 n = cell2intW (lexic, st->link[1]);
1017 if (n <= 0)
1018 return NULL;
1019
1020#ifdef STOP_AT_FIRST_ERROR
1021 for (tc1 = NULL, i = 1; i <= n; i++)
1022 {
1023 deref_cell (tc1);
1024 if ((tc1 = nasl_exec (lexic, st->link[0])) == NULL)
1025 return NULL;
1026 }
1027 return tc1;
1028#else
1029 for (i = 1; i <= n; i++)
1030 {
1031 tc1 = nasl_exec (lexic, st->link[0]);
1032 deref_cell (tc1);
1033 }
1034 return FAKE_CELL;
1035#endif
1036
1037 /*
1038 * I wonder...
1039 * Will nasl_exec be really called with NODE_EXEC or NODE_ARG?
1040 */
1041 case NODE_DECL: /* Used in function declarations */
1042 /* [0] = next arg in list */
1043 /* TBD? */
1044 return st; /* ? */
1045
1046 case NODE_ARG: /* Used function calls */
1047 /* val = name can be NULL, [0] = val, [1] = next arg */
1048 ret = nasl_exec (lexic, st->link[0]); /* Is this wise? */
1049 return ret;
1050
1051 case NODE_RETURN:
1052 /* [0] = ret val */
1053 ret = nasl_return (lexic, st->link[0]);
1054 return ret;
1055
1056 case NODE_BREAK:
1057 lexic->break_flag = 1;
1058 return FAKE_CELL;
1059
1060 case NODE_CONTINUE:
1061 lexic->cont_flag = 1;
1062 return FAKE_CELL;
1063
1064 case NODE_ARRAY_EL: /* val = array name, [0] = index */
1065 idx = cell2atom (lexic, st->link[0]);
1066 ret = get_array_elem (lexic, st->x.str_val, idx);
1067 deref_cell (idx);
1068 return ret;
1069
1072 case NODE_AFF:
1073 /* [0] = lvalue, [1] = rvalue */
1074 tc1 = nasl_exec (lexic, st->link[0]);
1075 tc2 = nasl_exec (lexic, st->link[1]);
1076 ret = nasl_affect (tc1, tc2);
1077 deref_cell (tc1); /* Must free VAR_REF */
1078 deref_cell (ret);
1079 return tc2; /* So that "a = b = e;" works */
1080
1081 case NODE_PLUS_EQ:
1082 tc1 = nasl_exec (lexic, st->link[0]);
1083 tc2 = nasl_exec (lexic, st->link[1]);
1084 tc3 = alloc_expr_cell (0, EXPR_PLUS, tc1, tc2);
1085 ret2 = nasl_exec (lexic, tc3);
1086 ret = nasl_affect (tc1, ret2);
1087 deref_cell (tc3); /* Frees tc1 and tc2 */
1088 deref_cell (ret);
1089 return ret2; /* So that "a = b += e;" works */
1090
1091 case NODE_MINUS_EQ:
1092 tc1 = nasl_exec (lexic, st->link[0]);
1093 tc2 = nasl_exec (lexic, st->link[1]);
1094 tc3 = alloc_expr_cell (0, EXPR_MINUS, tc1, tc2);
1095 ret2 = nasl_exec (lexic, tc3);
1096 ret = nasl_affect (tc1, ret2);
1097 deref_cell (tc3); /* Frees tc1 and tc2 */
1098 deref_cell (ret);
1099 return ret2; /* So that "a = b -= e;" works */
1100
1101 case NODE_MULT_EQ:
1102 tc1 = nasl_exec (lexic, st->link[0]);
1103 tc2 = nasl_exec (lexic, st->link[1]);
1104 tc3 = alloc_expr_cell (0, EXPR_MULT, tc1, tc2);
1105 ret2 = nasl_exec (lexic, tc3);
1106 ret = nasl_affect (tc1, ret2);
1107 deref_cell (tc3); /* Frees tc1 and tc2 */
1108 deref_cell (ret);
1109 return ret2;
1110
1111 case NODE_DIV_EQ:
1112 tc1 = nasl_exec (lexic, st->link[0]);
1113 tc2 = nasl_exec (lexic, st->link[1]);
1114 tc3 = alloc_expr_cell (0, EXPR_DIV, tc1, tc2);
1115 ret2 = nasl_exec (lexic, tc3);
1116 ret = nasl_affect (tc1, ret2);
1117 deref_cell (tc3); /* Frees tc1 and tc2 */
1118 deref_cell (ret);
1119 return ret2;
1120
1121 case NODE_MODULO_EQ:
1122 tc1 = nasl_exec (lexic, st->link[0]);
1123 tc2 = nasl_exec (lexic, st->link[1]);
1124 tc3 = alloc_expr_cell (0, EXPR_MODULO, tc1, tc2);
1125 ret2 = nasl_exec (lexic, tc3);
1126 ret = nasl_affect (tc1, ret2);
1127 deref_cell (tc3); /* Frees tc1 and tc2 */
1128 deref_cell (ret);
1129 return ret2;
1130
1131 case NODE_L_SHIFT_EQ:
1132 tc1 = nasl_exec (lexic, st->link[0]);
1133 tc2 = nasl_exec (lexic, st->link[1]);
1134 tc3 = alloc_expr_cell (0, EXPR_L_SHIFT, tc1, tc2);
1135 ret2 = nasl_exec (lexic, tc3);
1136 ret = nasl_affect (tc1, ret2);
1137 deref_cell (tc3); /* Frees tc1 and tc2 */
1138 deref_cell (ret);
1139 return ret2;
1140
1141 case NODE_R_SHIFT_EQ:
1142 tc1 = nasl_exec (lexic, st->link[0]);
1143 tc2 = nasl_exec (lexic, st->link[1]);
1144 tc3 = alloc_expr_cell (0, EXPR_R_SHIFT, tc1, tc2);
1145 ret2 = nasl_exec (lexic, tc3);
1146 ret = nasl_affect (tc1, ret2);
1147 deref_cell (tc3); /* Frees tc1 and tc2 */
1148 deref_cell (ret);
1149 return ret2;
1150
1151 case NODE_R_USHIFT_EQ:
1152 tc1 = nasl_exec (lexic, st->link[0]);
1153 tc2 = nasl_exec (lexic, st->link[1]);
1154 tc3 = alloc_expr_cell (0, EXPR_R_USHIFT, tc1, tc2);
1155 ret2 = nasl_exec (lexic, tc3);
1156 ret = nasl_affect (tc1, ret2);
1157 deref_cell (tc3); /* Frees tc1 and tc2 */
1158 deref_cell (ret);
1159 return ret2;
1160
1161 case NODE_VAR:
1162 /* val = variable name */
1163 ret = get_variable_by_name (lexic, st->x.str_val);
1164 return ret;
1165
1166 case NODE_LOCAL: /* [0] = argdecl */
1167 ret = decl_local_variables (lexic, st->link[0]);
1168 return ret;
1169
1170 case NODE_GLOBAL: /* [0] = argdecl */
1171 ret = decl_global_variables (lexic, st->link[0]);
1172 return ret;
1173
1174 case EXPR_AND:
1175 x = cell2bool (lexic, st->link[0]);
1176 if (!x)
1177 return bool2cell (0);
1178
1179 y = cell2bool (lexic, st->link[1]);
1180 return bool2cell (y);
1181
1182 case EXPR_OR:
1183 x = cell2bool (lexic, st->link[0]);
1184 if (x)
1185 return bool2cell (x);
1186 y = cell2bool (lexic, st->link[1]);
1187 return bool2cell (y);
1188
1189 case EXPR_NOT:
1190 x = cell2bool (lexic, st->link[0]);
1191 return bool2cell (!x);
1192
1193 case EXPR_INCR:
1194 case EXPR_DECR:
1195 x = (st->type == EXPR_INCR) ? 1 : -1;
1196 if (st->link[0] == NULL)
1197 {
1198 y = 1; /* pre */
1199 tc1 = st->link[1];
1200 }
1201 else
1202 {
1203 y = 0; /* post */
1204 tc1 = st->link[0];
1205 }
1206 tc2 = nasl_exec (lexic, tc1);
1207 if (tc2 == NULL)
1208 return NULL;
1209 ret = nasl_incr_variable (lexic, tc2, y, x);
1210 deref_cell (tc2);
1211 return ret;
1212
1213 case EXPR_PLUS:
1214 s1 = s2 = NULL;
1215 tc1 = cell2atom (lexic, st->link[0]);
1216#ifdef STOP_AT_FIRST_ERROR
1217 if (tc1 == NULL || tc1 == FAKE_CELL)
1218 return NULL;
1219#endif
1220 tc2 = cell2atom (lexic, st->link[1]);
1221 if (tc2 == NULL || tc2 == FAKE_CELL)
1222 {
1223#ifdef STOP_AT_FIRST_ERROR
1224 deref_cell (tc1);
1225 return NULL;
1226#else
1227 return tc1;
1228#endif
1229 }
1230
1231 if (tc1 == NULL || tc1 == FAKE_CELL)
1232 return tc2;
1233
1234 /*
1235 * Anything added to a string is converted to a string
1236 * Otherwise anything added to an intger is converted into an integer
1237 */
1238 if (tc1->type == CONST_DATA || tc2->type == CONST_DATA)
1239 flag = CONST_DATA;
1240 else if (tc1->type == CONST_STR || tc2->type == CONST_STR)
1241 flag = CONST_STR;
1242 else if (tc1->type == CONST_INT || tc2->type == CONST_INT)
1243 flag = CONST_INT;
1244 else
1245 flag = NODE_EMPTY;
1246 switch (flag)
1247 {
1248 long sz;
1249 case CONST_INT:
1250 x = tc1->x.i_val;
1251 y = cell2int (lexic, tc2);
1252 ret = int2cell (x + y);
1253 break;
1254
1255 case CONST_STR:
1256 case CONST_DATA:
1257 s1 = s2 = NULL;
1258 if (tc1->type == CONST_STR || tc1->type == CONST_DATA)
1259 len1 = tc1->size;
1260 else
1261 {
1262 s1 = cell2str (lexic, tc1);
1263 len1 = (s1 == NULL ? 0 : strlen (s1));
1264 }
1265
1266 if (tc2->type == CONST_STR || tc2->type == CONST_DATA)
1267 len2 = tc2->size;
1268 else
1269 {
1270 s2 = cell2str (lexic, tc2);
1271 len2 = (s2 == NULL ? 0 : strlen (s2));
1272 }
1273
1274 sz = len1 + len2;
1275 s3 = g_malloc0 (sz + 1);
1276 if (len1 > 0)
1277 memcpy (s3, s1 != NULL ? s1 : tc1->x.str_val, len1);
1278 if (len2 > 0)
1279 memcpy (s3 + len1, s2 != NULL ? s2 : tc2->x.str_val, len2);
1280 g_free (s1);
1281 g_free (s2);
1282 ret = alloc_typed_cell (flag);
1283 ret->x.str_val = s3;
1284 ret->size = sz;
1285 break;
1286
1287 default:
1288 ret = NULL;
1289 break;
1290 }
1291 deref_cell (tc1);
1292 deref_cell (tc2);
1293 return ret;
1294
1295 case EXPR_MINUS: /* Infamous duplicated code */
1296 s1 = s2 = NULL;
1297 tc1 = cell2atom (lexic, st->link[0]);
1298#ifdef STOP_AT_FIRST_ERROR
1299 if (tc1 == NULL || tc1 == FAKE_CELL)
1300 return NULL;
1301#endif
1302 tc2 = cell2atom (lexic, st->link[1]);
1303 if (tc2 == NULL || tc2 == FAKE_CELL)
1304 {
1305#ifdef STOP_AT_FIRST_ERROR
1306 deref_cell (tc1);
1307 return NULL;
1308#else
1309 return tc1;
1310#endif
1311 }
1312
1313 if (tc1 == NULL || tc1 == FAKE_CELL)
1314 {
1315 if (tc2->type == CONST_INT)
1316 {
1317 y = cell2int (lexic, tc2);
1318 ret = int2cell (-y);
1319 }
1320 else
1321 ret = NULL;
1322 deref_cell (tc2);
1323 return ret;
1324 }
1325
1326 /*
1327 * Anything subtracted from a string is converted to a string
1328 * Otherwise anything subtracted from integer is converted into an
1329 * integer
1330 */
1331 if (tc1->type == CONST_DATA || tc2->type == CONST_DATA)
1332 flag = CONST_DATA;
1333 else if (tc1->type == CONST_STR || tc2->type == CONST_STR)
1334 flag = CONST_STR;
1335 else if (tc1->type == CONST_INT || tc2->type == CONST_INT)
1336 flag = CONST_INT;
1337 else
1338 flag = NODE_EMPTY;
1339 switch (flag)
1340 {
1341 case CONST_INT:
1342 x = cell2int (lexic, tc1);
1343 y = cell2int (lexic, tc2);
1344 ret = int2cell (x - y);
1345 break;
1346
1347 case CONST_STR:
1348 case CONST_DATA:
1349 if (tc1->type == CONST_STR || tc1->type == CONST_DATA)
1350 {
1351 p1 = tc1->x.str_val;
1352 len1 = tc1->size;
1353 }
1354 else
1355 {
1356 p1 = s1 = cell2str (lexic, tc1);
1357 len1 = (s1 == NULL ? 0 : strlen (s1));
1358 }
1359
1360 if (tc2->type == CONST_STR || tc2->type == CONST_DATA)
1361 {
1362 p2 = tc2->x.str_val;
1363 len2 = tc2->size;
1364 }
1365 else
1366 {
1367 p2 = s2 = cell2str (lexic, tc2);
1368 len2 = (s2 == NULL ? 0 : strlen (s2));
1369 }
1370
1371 /* if p1 is null, last condition p=memem() will not be evaluated
1372 * and p remains NULL */
1373 if (len2 == 0 || len1 < len2
1374 || (p1 != NULL && (p = memmem (p1, len1, p2, len2)) == NULL))
1375 {
1376 s3 = g_malloc0 (len1 + 1);
1377 if (p1 != NULL)
1378 memcpy (s3, p1, len1);
1379 ret = alloc_typed_cell (flag);
1380 ret->x.str_val = s3;
1381 ret->size = len1;
1382 }
1383 else
1384 {
1385 long sz = len1 - len2;
1386 if (sz <= 0)
1387 {
1388 sz = 0;
1389 s3 = g_strdup ("");
1390 }
1391 else
1392 {
1393 s3 = g_malloc0 (sz + 1);
1394 if (p - p1 > 0)
1395 memcpy (s3, p1, p - p1);
1396 if (p != NULL && sz > p - p1)
1397 memcpy (s3 + (p - p1), p + len2, sz - (p - p1));
1398 }
1399 ret = alloc_typed_cell (flag);
1400 ret->x.str_val = s3;
1401 ret->size = sz;
1402 }
1403
1404 g_free (s1);
1405 g_free (s2);
1406 break;
1407
1408 default:
1409 ret = NULL;
1410 break;
1411 }
1412 deref_cell (tc1);
1413 deref_cell (tc2);
1414 return ret;
1415
1416 case EXPR_MULT:
1417 x = cell2intW (lexic, st->link[0]);
1418 y = cell2intW (lexic, st->link[1]);
1419 return int2cell (x * y);
1420
1421 case EXPR_DIV:
1422 x = cell2intW (lexic, st->link[0]);
1423 y = cell2intW (lexic, st->link[1]);
1424 if (y != 0)
1425 return int2cell (x / y);
1426 else
1427 return int2cell (0);
1428
1429 case EXPR_EXPO:
1430 x = cell2intW (lexic, st->link[0]);
1431 y = cell2intW (lexic, st->link[1]);
1432 return int2cell (expo (x, y));
1433
1434 case EXPR_MODULO:
1435 x = cell2intW (lexic, st->link[0]);
1436 y = cell2intW (lexic, st->link[1]);
1437 if (y != 0)
1438 return int2cell (x % y);
1439 else
1440 return int2cell (0);
1441
1442 case EXPR_BIT_AND:
1443 x = cell2intW (lexic, st->link[0]);
1444 y = cell2intW (lexic, st->link[1]);
1445 return int2cell (x & y);
1446
1447 case EXPR_BIT_OR:
1448 x = cell2intW (lexic, st->link[0]);
1449 y = cell2intW (lexic, st->link[1]);
1450 return int2cell (x | y);
1451
1452 case EXPR_BIT_XOR:
1453 x = cell2intW (lexic, st->link[0]);
1454 y = cell2intW (lexic, st->link[1]);
1455 return int2cell (x ^ y);
1456
1457 case EXPR_BIT_NOT:
1458 x = cell2intW (lexic, st->link[0]);
1459 return int2cell (~x);
1460
1461 case EXPR_U_MINUS:
1462 x = cell2intW (lexic, st->link[0]);
1463 return int2cell (-x);
1464
1465 /* TBD: Handle shift for strings and arrays */
1466 case EXPR_L_SHIFT:
1467 x = cell2intW (lexic, st->link[0]);
1468 y = cell2intW (lexic, st->link[1]);
1469 return int2cell (x << y);
1470
1471 case EXPR_R_SHIFT: /* arithmetic right shift */
1472 x = cell2intW (lexic, st->link[0]);
1473 y = cell2intW (lexic, st->link[1]);
1474 z = x >> y;
1475#ifndef __GNUC__
1476 if (x < 0 && z >= 0) /* Fix it */
1477 z |= (~0) << (sizeof (x) * 8 - y);
1478#endif
1479 return int2cell (z);
1480
1481 case EXPR_R_USHIFT:
1482 x = cell2intW (lexic, st->link[0]);
1483 y = cell2intW (lexic, st->link[1]);
1484 z = (unsigned) x >> (unsigned) y;
1485#ifndef __GNUC__
1486 if (x < 0 && z <= 0) /* Fix it! */
1487 z &= ~((~0) << (sizeof (x) * 8 - y));
1488#endif
1489 return int2cell (z);
1490
1491 case COMP_MATCH:
1492 case COMP_NOMATCH:
1493 tc1 = cell2atom (lexic, st->link[0]);
1494 tc2 = cell2atom (lexic, st->link[1]);
1495 s1 = s2 = NULL;
1496
1497 if (tc1 == NULL || tc1 == FAKE_CELL)
1498 {
1499 p1 = "";
1500 len1 = 0;
1501 }
1502 else if (tc1->type == CONST_STR || tc1->type == CONST_DATA)
1503 {
1504 p1 = tc1->x.str_val;
1505 len1 = tc1->size;
1506 }
1507 else
1508 {
1509 p1 = s1 = cell2str (lexic, tc1);
1510 len1 = strlen (s1);
1511 }
1512
1513 if (tc2 == NULL || tc2 == FAKE_CELL)
1514 {
1515 p2 = "";
1516 len2 = 0;
1517 }
1518 else if (tc2->type == CONST_STR || tc2->type == CONST_DATA)
1519 {
1520 p2 = tc2->x.str_val;
1521 len2 = tc2->size;
1522 }
1523 else
1524 {
1525 p2 = s2 = cell2str (lexic, tc2);
1526 len2 = strlen (s2);
1527 }
1528
1529 if (len1 <= len2)
1530 flag = (memmem (p2, len2, p1, len1) != NULL);
1531 else
1532 flag = 0;
1533
1534 g_free (s1);
1535 g_free (s2);
1536 deref_cell (tc1);
1537 deref_cell (tc2);
1538 if (st->type == COMP_MATCH)
1539 return bool2cell (flag);
1540 else
1541 return bool2cell (!flag);
1542
1543 case COMP_RE_MATCH:
1544 case COMP_RE_NOMATCH:
1545 if (st->x.ref_val == NULL)
1546 {
1547 nasl_perror (lexic, "nasl_exec: bad regex at or near line %d\n",
1548 st->line_nb);
1549 return NULL;
1550 }
1551 s1 = cell2str (lexic, st->link[0]);
1552 if (s1 == NULL)
1553 return 0;
1554 flag = regexec (st->x.ref_val, s1, 0, NULL, 0);
1555 g_free (s1);
1556 if (st->type == COMP_RE_MATCH)
1557 return bool2cell (flag != REG_NOMATCH);
1558 else
1559 return bool2cell (flag == REG_NOMATCH);
1560
1561 case COMP_LT:
1562 return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) < 0);
1563
1564 case COMP_LE:
1565 return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) <= 0);
1566
1567 case COMP_EQ:
1568 return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) == 0);
1569
1570 case COMP_NE:
1571 return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) != 0);
1572
1573 case COMP_GT:
1574 return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) > 0);
1575
1576 case COMP_GE:
1577 return bool2cell (cell_cmp (lexic, st->link[0], st->link[1]) >= 0);
1578
1579 case REF_ARRAY:
1580 case DYN_ARRAY:
1581 case CONST_INT:
1582 case CONST_STR:
1583 case CONST_DATA:
1584 ref_cell (st); /* nasl_exec returns a cell that should be deref-ed */
1585 return st;
1586
1587 case REF_VAR:
1588 ret = nasl_read_var_ref (lexic, st);
1589 return ret;
1590
1591 default:
1592 nasl_perror (lexic, "nasl_exec: unhandled node type %d\n", st->type);
1593 abort ();
1594 return NULL;
1595 }
1596}
FILE * nasl_trace_fp
Definition exec.c:357
long int cell_cmp(lex_ctxt *lexic, tree_cell *c1, tree_cell *c2)
Definition exec.c:218
static long int cell2intW(lex_ctxt *lexic, tree_cell *c)
Definition exec.c:130
static void nasl_short_dump(FILE *fp, const tree_cell *c)
Definition exec.c:651
static long int expo(long int x, long int y)
Definition exec.c:745
static tree_cell * bool2cell(long int x)
Definition exec.c:144
static int cell2bool(lex_ctxt *lexic, tree_cell *c)
Definition exec.c:44
static tree_cell * int2cell(long int x)
Definition exec.c:136
tree_cell * nasl_func_call(lex_ctxt *lexic, const nasl_func *f, tree_cell *arg_list)
Definition nasl_func.c:95
tree_cell * decl_nasl_func(lex_ctxt *lexic, tree_cell *decl_node, int lint_mode)
Definition nasl_func.c:66
nasl_func * get_func_ref_by_name(lex_ctxt *ctxt, const char *name)
Definition nasl_func.c:82
tree_cell * nasl_return(lex_ctxt *ctxt, tree_cell *retv)
Definition nasl_func.c:236
struct st_nasl_func nasl_func
const char * val
Definition nasl_init.c:441
tree_cell * nasl_incr_variable(lex_ctxt *, tree_cell *, int, int)
Definition nasl_var.c:919
tree_cell * get_array_elem(lex_ctxt *, const char *, tree_cell *)
Definition nasl_var.c:208
tree_cell * nasl_read_var_ref(lex_ctxt *, tree_cell *)
Definition nasl_var.c:832
tree_cell * decl_global_variables(lex_ctxt *, tree_cell *)
Definition nasl_var.c:771
tree_cell * decl_local_variables(lex_ctxt *, tree_cell *)
Definition nasl_var.c:758
tree_cell * get_variable_by_name(lex_ctxt *, const char *)
Definition nasl_var.c:176
tree_cell * alloc_expr_cell(int lnb, int t, tree_cell *l, tree_cell *r)
Definition nasl_tree.c:60
tree_cell * alloc_typed_cell(int typ)
Definition nasl_tree.c:25
@ NODE_FOR
Definition nasl_tree.h:16
@ NODE_ARRAY_EL
Definition nasl_tree.h:29
@ NODE_L_SHIFT_EQ
Definition nasl_tree.h:41
@ NODE_PLUS_EQ
Definition nasl_tree.h:35
@ NODE_FUN_CALL
Definition nasl_tree.h:22
@ COMP_LE
Definition nasl_tree.h:73
@ NODE_R_SHIFT_EQ
Definition nasl_tree.h:42
@ COMP_GT
Definition nasl_tree.h:76
@ NODE_LOCAL
Definition nasl_tree.h:32
@ NODE_AFF
Definition nasl_tree.h:30
@ EXPR_MODULO
Definition nasl_tree.h:54
@ COMP_RE_MATCH
Definition nasl_tree.h:69
@ NODE_MULT_EQ
Definition nasl_tree.h:37
@ COMP_NE
Definition nasl_tree.h:75
@ NODE_VAR
Definition nasl_tree.h:31
@ NODE_R_USHIFT_EQ
Definition nasl_tree.h:43
@ NODE_ARG
Definition nasl_tree.h:24
@ NODE_CONTINUE
Definition nasl_tree.h:27
@ COMP_EQ
Definition nasl_tree.h:74
@ EXPR_MULT
Definition nasl_tree.h:52
@ NODE_FUN_DEF
Definition nasl_tree.h:21
@ COMP_GE
Definition nasl_tree.h:77
@ NODE_REPEAT_UNTIL
Definition nasl_tree.h:19
@ NODE_REPEATED
Definition nasl_tree.h:20
@ REF_VAR
Definition nasl_tree.h:88
@ EXPR_BIT_NOT
Definition nasl_tree.h:60
@ EXPR_R_SHIFT
Definition nasl_tree.h:64
@ EXPR_DIV
Definition nasl_tree.h:53
@ COMP_RE_NOMATCH
Definition nasl_tree.h:70
@ NODE_RETURN
Definition nasl_tree.h:25
@ NODE_EMPTY
Definition nasl_tree.h:13
@ EXPR_BIT_XOR
Definition nasl_tree.h:59
@ NODE_FOREACH
Definition nasl_tree.h:18
@ NODE_IF_ELSE
Definition nasl_tree.h:14
@ NODE_DIV_EQ
Definition nasl_tree.h:38
@ NODE_MINUS_EQ
Definition nasl_tree.h:36
@ COMP_NOMATCH
Definition nasl_tree.h:68
@ COMP_MATCH
Definition nasl_tree.h:67
@ EXPR_NOT
Definition nasl_tree.h:47
@ EXPR_PLUS
Definition nasl_tree.h:49
@ EXPR_L_SHIFT
Definition nasl_tree.h:63
@ NODE_DECL
Definition nasl_tree.h:23
@ EXPR_BIT_AND
Definition nasl_tree.h:57
@ EXPR_EXPO
Definition nasl_tree.h:55
@ EXPR_INCR
Definition nasl_tree.h:61
@ NODE_INSTR_L
Definition nasl_tree.h:15
@ EXPR_BIT_OR
Definition nasl_tree.h:58
@ EXPR_U_MINUS
Definition nasl_tree.h:51
@ COMP_LT
Definition nasl_tree.h:72
@ EXPR_OR
Definition nasl_tree.h:46
@ NODE_BREAK
Definition nasl_tree.h:26
@ EXPR_DECR
Definition nasl_tree.h:62
@ EXPR_R_USHIFT
Definition nasl_tree.h:65
@ NODE_GLOBAL
Definition nasl_tree.h:33
@ NODE_WHILE
Definition nasl_tree.h:17
@ EXPR_MINUS
Definition nasl_tree.h:50
@ NODE_MODULO_EQ
Definition nasl_tree.h:39
@ EXPR_AND
Definition nasl_tree.h:45
nasl_iterator nasl_array_iterator(void *ctxt, tree_cell *c)
Definition nasl_var.c:1169
tree_cell * nasl_iterate_array(nasl_iterator *it)
Definition nasl_var.c:1205
tree_cell * nasl_affect(tree_cell *lval, tree_cell *rval)
Definition nasl_var.c:697
void free_array(nasl_array *a)
Definition nasl_var.c:339
struct TC * link[4]
Definition nasl_tree.h:107
union TC::@332262321161220155002104006201360276211317150140 x
short line_nb
Definition nasl_tree.h:96
char * str_val
Definition nasl_tree.h:103
void * ref_val
Definition nasl_tree.h:105
nasl_array * a
Definition nasl_var.h:67
tree_cell * ret_val
unsigned break_flag

References nasl_iterator::a, alloc_expr_cell(), alloc_typed_cell(), bool2cell(), struct_lex_ctxt::break_flag, cell2atom(), cell2bool(), cell2int(), cell2intW(), cell2str(), cell_cmp(), COMP_EQ, COMP_GE, COMP_GT, COMP_LE, COMP_LT, COMP_MATCH, COMP_NE, COMP_NOMATCH, COMP_RE_MATCH, COMP_RE_NOMATCH, CONST_DATA, CONST_INT, CONST_STR, struct_lex_ctxt::cont_flag, decl_global_variables(), decl_local_variables(), decl_nasl_func(), deref_cell(), DYN_ARRAY, expo(), EXPR_AND, EXPR_BIT_AND, EXPR_BIT_NOT, EXPR_BIT_OR, EXPR_BIT_XOR, EXPR_DECR, EXPR_DIV, EXPR_EXPO, EXPR_INCR, EXPR_L_SHIFT, EXPR_MINUS, EXPR_MODULO, EXPR_MULT, EXPR_NOT, EXPR_OR, EXPR_PLUS, EXPR_R_SHIFT, EXPR_R_USHIFT, EXPR_U_MINUS, FAKE_CELL, free_array(), get_array_elem(), get_func_ref_by_name(), get_variable_by_name(), int2cell(), struct_lex_ctxt::line_nb, TC::line_nb, TC::link, nasl_affect(), nasl_array_iterator(), nasl_exec(), nasl_func_call(), nasl_incr_variable(), nasl_iterate_array(), nasl_perror(), nasl_read_var_ref(), nasl_return(), nasl_short_dump(), nasl_trace_fp, NODE_AFF, NODE_ARG, NODE_ARRAY_EL, NODE_BREAK, NODE_CONTINUE, NODE_DECL, NODE_DIV_EQ, NODE_EMPTY, NODE_FOR, NODE_FOREACH, NODE_FUN_CALL, NODE_FUN_DEF, NODE_GLOBAL, NODE_IF_ELSE, NODE_INSTR_L, NODE_L_SHIFT_EQ, NODE_LOCAL, NODE_MINUS_EQ, NODE_MODULO_EQ, NODE_MULT_EQ, NODE_PLUS_EQ, NODE_R_SHIFT_EQ, NODE_R_USHIFT_EQ, NODE_REPEAT_UNTIL, NODE_REPEATED, NODE_RETURN, NODE_VAR, NODE_WHILE, REF_ARRAY, ref_cell(), TC::ref_val, REF_VAR, struct_lex_ctxt::ret_val, TC::size, TC::str_val, TC::type, val, and TC::x.

Referenced by cell2atom(), cell2bool(), cell2int3(), cell2str(), exec_nasl_script(), nasl_exec(), and nasl_func_call().

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