Skip to content

Commit 1018c36

Browse files
committed
Merge remote-tracking branch 'remotes/Daniel-Cortez/error-094' into dev
2 parents 555027f + 313b071 commit 1018c36

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

source/compiler/sc3.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,10 @@ static void plnge2(void (*oper)(void),
620620
} /* if */
621621
/* ??? ^^^ should do same kind of error checking with functions */
622622

623+
/* If we're handling a division operation, make sure the divisor is not zero. */
624+
if ((oper==os_div || oper==os_mod) && lval2->ident==iCONSTEXPR && lval2->constval==0)
625+
error(94); /* division by zero */
626+
623627
/* check whether an "operator" function is defined for the tag names
624628
* (a constant expression cannot be optimized in that case)
625629
*/
@@ -648,10 +652,8 @@ static cell flooreddiv(cell a,cell b,int return_remainder)
648652
{
649653
cell q,r;
650654

651-
if (b==0) {
652-
error(29);
655+
if (b==0)
653656
return 0;
654-
} /* if */
655657
/* first implement truncated division in a portable way */
656658
#define IABS(a) ((a)>=0 ? (a) : (-a))
657659
q=IABS(a)/IABS(b);

source/compiler/sc5.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ static char *errmsg[] = {
132132
/*090*/ "public functions may not return arrays (symbol \"%s\")\n",
133133
/*091*/ "ambiguous constant; tag override is required (symbol \"%s\")\n",
134134
/*092*/ "functions may not return arrays of unknown size (symbol \"%s\")\n",
135-
/*093*/ "\"__addressof\" operator is invalid in preprocessor expressions\n"
135+
/*093*/ "\"__addressof\" operator is invalid in preprocessor expressions\n",
136+
/*094*/ "division by zero\n"
136137
};
137138

138139
static char *fatalmsg[] = {

source/compiler/tests/error_094.meta

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
'test_type': 'output_check',
3+
'errors': """
4+
error_094.pwn(6) : error 094: division by zero
5+
error_094.pwn(7) : error 094: division by zero
6+
error_094.pwn(13) : error 094: division by zero
7+
error_094.pwn(14) : error 094: division by zero
8+
"""
9+
}

source/compiler/tests/error_094.pwn

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <console>
2+
3+
main()
4+
{
5+
// Case 1: Both operands are compile-time constants
6+
printf("%d", 1 / 0); // error 094
7+
printf("%d", 1 % 0); // error 094
8+
printf("%d", 1 / 1);
9+
printf("%d", 1 % 1);
10+
11+
// Case 2: Only the divisor is a constant
12+
new var = 0;
13+
printf("%d", var / 0); // error 094
14+
printf("%d", var % 0); // error 094
15+
printf("%d", var / 1);
16+
printf("%d", var % 1);
17+
18+
printf("%d", 1 / var); // Just to make sure the error works only
19+
printf("%d", 1 % var); // if the divisor is a constant value
20+
}

0 commit comments

Comments
 (0)