Skip to content

Commit c98a254

Browse files
committed
COMMON: fix NPE evaluating invalid code
1 parent 9242a56 commit c98a254

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

src/common/eval.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -426,38 +426,38 @@ void eval(var_t *r) {
426426
op = CODE(IP);
427427
IP++;
428428

429-
if (r->type == V_INT && left->type == V_INT) {
429+
if (r->type == V_INT && v_is_type(left, V_INT)) {
430430
if (op == '+') {
431431
r->v.i += left->v.i;
432432
} else {
433433
r->v.i = left->v.i - r->v.i;
434434
}
435-
} else if (r->type == V_NUM && left->type == V_NUM) {
435+
} else if (r->type == V_NUM && v_is_type(left, V_NUM)) {
436436
r->type = V_NUM;
437437
if (op == '+') {
438438
r->v.n += left->v.n;
439439
} else {
440440
r->v.n = left->v.n - r->v.n;
441441
}
442-
} else if (r->type == V_INT && left->type == V_NUM) {
442+
} else if (r->type == V_INT && v_is_type(left, V_NUM)) {
443443
r->type = V_NUM;
444444
if (op == '+') {
445445
r->v.n = r->v.i + left->v.n;
446446
} else {
447447
r->v.n = left->v.n - r->v.i;
448448
}
449-
} else if (r->type == V_NUM && left->type == V_INT) {
449+
} else if (r->type == V_NUM && v_is_type(left, V_INT)) {
450450
if (op == '+') {
451451
r->v.n += left->v.i;
452452
} else {
453453
r->v.n = left->v.i - r->v.n;
454454
}
455455
} else {
456-
if (r->type == V_ARRAY || left->type == V_ARRAY) {
456+
if (r->type == V_ARRAY || v_is_type(left, V_ARRAY)) {
457457
//
458458
// ARRAYS
459459
//
460-
if (r->type == V_ARRAY && left->type == V_ARRAY) {
460+
if (r->type == V_ARRAY && v_is_type(left, V_ARRAY)) {
461461
if (op == '+') {
462462
mat_add(r, left);
463463
} else {
@@ -499,11 +499,11 @@ void eval(var_t *r) {
499499
op = CODE(IP);
500500
IP++;
501501

502-
if (r->type == V_ARRAY || left->type == V_ARRAY) {
502+
if (r->type == V_ARRAY || v_is_type(left, V_ARRAY)) {
503503
//
504504
// ARRAYS
505505
//
506-
if (r->type == V_ARRAY && left->type == V_ARRAY) {
506+
if (r->type == V_ARRAY && v_is_type(left, V_ARRAY)) {
507507
if (op == '*') {
508508
mat_mul(left, r);
509509
} else {
@@ -769,13 +769,13 @@ void eval(var_t *r) {
769769
}
770770
}
771771
} else if (r->type == V_STR) {
772-
if (left->type == V_STR) {
772+
if (v_is_type(left, V_STR)) {
773773
if (left->v.p.ptr[0] != '\0') {
774774
ri = (strstr(r->v.p.ptr, left->v.p.ptr) != NULL);
775775
} else {
776776
ri = 0;
777777
}
778-
} else if (left->type == V_NUM || left->type == V_INT) {
778+
} else if (v_is_type(left, V_NUM) || v_is_type(left, V_INT)) {
779779
var_t *v;
780780

781781
v = v_clone(left);

src/common/extlib.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ int slib_llopen(slib_t * lib) {
277277
return (lib->handle != NULL);
278278
#elif defined(__CYGWIN__)
279279
char win32Path[1024];
280-
cygwin_conv_to_full_win32_path(lib->fullname, win32Path);
280+
cygwin_conv_path(CCP_POSIX_TO_WIN_A, lib->fullname, win32Path, sizeof(win32Path));
281281
lib->handle = LoadLibrary(win32Path);
282282
if (lib->handle == NULL)
283283
panic("SB-LibMgr: error on loading %s\n", win32Path);

src/common/inet.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@
1414
#include <config.h>
1515
#endif
1616

17-
#if defined(_Win32)
18-
#include <winsock.h>
19-
#else
2017
#include <string.h>
2118
#include <errno.h>
2219
#include <sys/types.h>
23-
#endif
2420

2521
typedef int socket_t;
2622

src/common/var.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,3 +925,10 @@ void v_input2var(const char *str, var_t *var) {
925925
tmp_free(sb);
926926
}
927927
}
928+
929+
/*
930+
* null safe variable type check
931+
*/
932+
int v_is_type(var_t *v, int type) {
933+
return (v != NULL && v->type == type);
934+
}

src/common/var.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,16 @@ void v_input2var(const char *str, var_t *var);
708708
*/
709709
char *v_getstr(var_t *v);
710710

711+
/**
712+
* @ingroup var
713+
*
714+
* returns whether the variable is of the given type
715+
*
716+
* @param v is the variable
717+
* @return whether the variable is of the given type
718+
*/
719+
int v_is_type(var_t *v, int type);
720+
711721
/*
712722
* low-level byte-code parsing
713723
*

0 commit comments

Comments
 (0)