Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(structs): fix segfault when using structs in boolean expression #1665

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 28 additions & 12 deletions Engine/csound_orc_expressions.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ int is_expression_node(TREE *node)
case '^':
case T_FUNCTION:
case S_UMINUS:
case S_UPLUS:
case S_UPLUS:
case '|':
case '&':
case S_BITSHIFT_RIGHT:
Expand Down Expand Up @@ -692,7 +692,7 @@ static TREE *create_expression(CSOUND *csound, TREE *root, int line, int locn,
root->right, typeTable);

break;

case '|':
strNcpy(op, "##or", 80);
outarg = create_out_arg_for_expression(csound, op, root->left,
Expand Down Expand Up @@ -771,7 +771,7 @@ static TREE *create_expression(CSOUND *csound, TREE *root, int line, int locn,
outype = strdup(var->subType->varTypeName);
/* VL: 9.2.22 pulled code from 6.x to check for array index type
to provide the correct outype. Works with explicity types
*/
*/
if (outype[0]== 'i') {
TREE* inds = root->right;
while (inds) {
Expand Down Expand Up @@ -837,6 +837,24 @@ static TREE *create_expression(CSOUND *csound, TREE *root, int line, int locn,
return anchor;
}

static int is_boolean_node_perf_rate(
CSOUND* csound,
TREE* node,
TYPE_TABLE* typeTable
) {
if (node == NULL) {
return 0;
}

if (node->type == STRUCT_EXPR) {
char* argtypStr = get_arg_type2(csound, node, typeTable);
return strlen(argtypStr) > 0 ? argtypStr[0] == 'k' || argtypStr[0] == 'B' : 0;
} else {
int argtypVal = argtyp2(node->value->lexeme);
return argtypVal == 'k' || argtypVal == 'B';
}
}

/**
* Create a chain of Opcode (OPTXT) text from the AST node given. Called from
* create_opcode when an expression node has been found as an argument
Expand Down Expand Up @@ -967,16 +985,14 @@ static TREE *create_boolean_expression(CSOUND *csound, TREE *root,
if (root->type == S_UNOT) {
outarg = get_boolean_arg(csound,
typeTable,
argtyp2( root->left->value->lexeme) =='k' ||
argtyp2( root->left->value->lexeme) =='B');
}
else
is_boolean_node_perf_rate(csound, root->left, typeTable));
} else {
outarg = get_boolean_arg(csound,
typeTable,
argtyp2( root->left->value->lexeme) =='k' ||
argtyp2( root->right->value->lexeme)=='k' ||
argtyp2( root->left->value->lexeme) =='B' ||
argtyp2( root->right->value->lexeme)=='B');
is_boolean_node_perf_rate(csound, root->left, typeTable) ||
is_boolean_node_perf_rate(csound, root->right, typeTable));
}


add_arg(csound, outarg, NULL, typeTable);
opTree = create_opcode_token(csound, op);
Expand Down Expand Up @@ -1434,7 +1450,7 @@ TREE* expand_until_statement(CSOUND* csound, TREE* current,
gotoType =
last->left->value->lexeme[1] == 'B'; // checking for #B... var name

// printf("%s\n", last->left->value->lexeme);
// printf("%s\n", last->left->value->lexeme);
//printf("gottype = %d ; dowhile = %d\n", gotoType, dowhile);
gotoToken =
create_goto_token(csound,
Expand Down
2 changes: 1 addition & 1 deletion Engine/csound_type_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ CS_VARIABLE* csoundCreateVariable(CSOUND* csound, TYPE_POOL* pool,
current = current->next;
}
else ((CSOUND *)csound)->ErrorMsg(csound,
Str("cannot create variable %s: NULL type"),
Str("cannot create variable %s: NULL type\n"),
name);
return NULL;
}
Expand Down
42 changes: 22 additions & 20 deletions tests/commandline/structs/test_structs.csd
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,38 @@ struct MyType imaginary:k, real:k, kimaginary, kreal

opcode processMyType(in:MyType):(MyType)
retVal:MyType init 0, 0, 0, 0
retVal.imaginary = in.imaginary + 1
retVal.real = in.real + 1
retVal.imaginary = in.imaginary + 1
retVal.real = in.real + 1
retVal.kimaginary = in.kimaginary + 1
retVal.kreal = in.kreal + 1
retVal.kreal = in.kreal + 1
xout retVal
endop

instr 1
instr 1

var0:MyType init 1, 2, 3, 4
;var1:MyType = init:MyType(0, 0, 1, 1)
var0:MyType init 1, 2, 3, 4
;var1:MyType = init:MyType(0, 0, 1, 1)

var0.imaginary init 5
var0.real init 6
var0.kimaginary init 7
var0.kreal init 9
var0.imaginary init 5
var0.real init 6
var0.kimaginary init 7
var0.kreal init 9

var0.imaginary += 1
var0.real += 2
var0.kimaginary += 3
var0.kreal += 4
var0.imaginary += 1
var0.real += 2
var0.kimaginary += 3
var0.kreal += 4

var2:MyType processMyType var0
var2:MyType processMyType var0

; this does not work yet...
;var2:MyType = processMyType(var0)
if var0.kreal > var0.real then
prints "structs in boolean expr work!\n"
endif
; this does not work yet...
;var2:MyType = processMyType(var0)

printks "i %d r %d ki %d kr %d\n", 0.2, var0.imaginary, var0.real, var0.kimaginary, var0.kreal
printks "\ti %d r %d ki %d kr %d\n", 0.2, var2.imaginary, var2.real, var2.kimaginary, var2.kreal
printks "i %d r %d ki %d kr %d\n", 0.2, var0.imaginary, var0.real, var0.kimaginary, var0.kreal
printks "\ti %d r %d ki %d kr %d\n", 0.2, var2.imaginary, var2.real, var2.kimaginary, var2.kreal

endin

Expand All @@ -50,4 +53,3 @@ i1 0 0.5

</CsScore>
</CsoundSynthesizer>