Skip to content

Commit

Permalink
zero div
Browse files Browse the repository at this point in the history
  • Loading branch information
thradams committed Jun 25, 2024
1 parent f097ee1 commit c38394d
Show file tree
Hide file tree
Showing 9 changed files with 4,782 additions and 4,638 deletions.
29 changes: 2 additions & 27 deletions src/file.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
#pragma safety enable
#if 54/0

#include <ownership.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#endif

struct X {
char * _Owner _Opt s;
};
void destroy(struct X * obj_owner p) {free(p->s);}


void f2(){
struct X* _Opt p = 0;

{
struct X x = {0};
p = &x;
x.s = strdup("a");
destroy(&x);
}
static_debug(p->s);
printf("%s", p->s);
}

int main(){

}
8 changes: 8 additions & 0 deletions src/flow_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ bool flow_object_is_not_zero(struct flow_object* p)
(e & OBJECT_STATE_NOT_ZERO));
}


bool flow_object_can_be_zero(const struct flow_object* p)
{
enum object_state e = p->current.state;

return (e & OBJECT_STATE_ZERO);
}

bool flow_object_can_be_null(struct flow_object* p)
{
enum object_state e = p->current.state;
Expand Down
2 changes: 2 additions & 0 deletions src/flow_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ bool flow_object_can_be_not_null_or_moved(struct flow_object* p);

bool flow_object_is_null(struct flow_object* p);
bool flow_object_can_be_null(struct flow_object* p);
bool flow_object_can_be_zero(const struct flow_object* p);



bool flow_object_is_not_zero(struct flow_object* p);
Expand Down
28 changes: 26 additions & 2 deletions src/flow_visit.c
Original file line number Diff line number Diff line change
Expand Up @@ -2106,10 +2106,34 @@ static void flow_visit_expression(struct flow_visit_ctx* ctx, struct expression*
//object_destroy(&temp_obj2);
}
break;
case MULTIPLICATIVE_EXPRESSION_DIV:
{
if (p_expression->left)
{
struct declarator_array left_set = { 0 };
flow_visit_expression(ctx, p_expression->left, &left_set);
declarator_array_destroy(&left_set);
}

if (p_expression->right)
{
struct declarator_array right_set = { 0 };
flow_visit_expression(ctx, p_expression->right, &right_set);
declarator_array_destroy(&right_set);

struct flow_object* p_object = expression_get_object(ctx, p_expression->right, ctx->ctx->options.null_checks_enabled);
if (p_object)
{
if (flow_object_can_be_zero(p_object))
{
compiler_diagnostic_message(W_DIVIZION_BY_ZERO, ctx->ctx, p_expression->right->first_token, "possible division by zero");
}
}
}
}
break;
case CAST_EXPRESSION:
case MULTIPLICATIVE_EXPRESSION_MULT:
case MULTIPLICATIVE_EXPRESSION_DIV:
case MULTIPLICATIVE_EXPRESSION_MULT:
case MULTIPLICATIVE_EXPRESSION_MOD:
case ADDITIVE_EXPRESSION_PLUS:
case ADDITIVE_EXPRESSION_MINUS:
Expand Down
Loading

0 comments on commit c38394d

Please sign in to comment.