Skip to content

Commit

Permalink
preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
thradams committed Mar 24, 2024
1 parent 7ba7775 commit 2561775
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 31 deletions.
42 changes: 29 additions & 13 deletions src/file.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
void free(void* _Owner _Opt p);
struct X
{
int i;
void* _Owner p;
void* _Owner malloc(unsigned long size);
void free(void* _Owner ptr);

struct Y {
char * _Owner p0;
int * _Owner p2;
double i2;
};
void f(struct X* p);
int main()
{
struct X x = { 0 };
static_state(x.p, "null");
f(&x);
static_state(x.p, "maybe-null");
free(x.p);

struct X {
char * _Owner text;
int * _Owner p1;
int i;
struct Y *pY;
};

void init(struct X * p);

int main() {
struct X x = {0};
static_debug(x);
init(&x);
static_debug(x);
static_state(x.p1, "maybe-null");
static_state(x.i, "any");
static_state(x.pY, "maybe-null");
static_state(x.pY->p0, "maybe-null");
static_state(x.pY->p2, "maybe-null");
static_state(x.pY->i2, "any");
free(x);
}
51 changes: 42 additions & 9 deletions src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -22299,7 +22299,7 @@ void checked_empty(struct parser_ctx* ctx,
compiler_diagnostic_message(W_OWNERSHIP_FLOW_MOVED,
ctx,
position_token,
"object '%s' it not empty",
"object '%s' is not empty",
name);
}
}
Expand Down Expand Up @@ -22351,6 +22351,7 @@ void object_set_moved(struct type* p_type, struct object* p_object)
}
return;
}
return;
}

if (type_is_pointer(p_type))
Expand Down Expand Up @@ -22416,6 +22417,7 @@ void object_set_unknown(struct type* p_type, struct object* p_object)
}
return;
}
return;
}

if (type_is_pointer(p_type))
Expand All @@ -22432,7 +22434,8 @@ void object_set_unknown(struct type* p_type, struct object* p_object)
}
else
{
p_object->state = OBJECT_STATE_ZERO | OBJECT_STATE_NOT_ZERO;
if (!type_is_struct_or_union(p_type))
p_object->state = OBJECT_STATE_ZERO | OBJECT_STATE_NOT_ZERO;
}
}

Expand Down Expand Up @@ -22483,6 +22486,7 @@ void object_set_zero(struct type* p_type, struct object* p_object)
}
return;
}
return;
}

if (type_is_pointer(p_type))
Expand Down Expand Up @@ -23355,7 +23359,7 @@ void object_assignment3(struct parser_ctx* ctx,
error_position,
"assignment of possible null object '%s' to non-opt pointer", buffer);
#endif //nullchecks disabled for now
}
}

if (type_is_owner(p_a_type) && type_is_pointer(p_a_type))
{
Expand All @@ -23370,17 +23374,24 @@ void object_assignment3(struct parser_ctx* ctx,
}
}

/*copying to void * owner*/
if (type_is_void_ptr(p_a_type) && type_is_pointer(p_b_type))
{
if (type_is_owner(p_a_type) && object_get_pointed_object(p_b_object))
if (type_is_owner(p_a_type))
{
struct type t = type_remove_pointer(p_b_type);
checked_empty(ctx, &t, object_get_pointed_object(p_b_object), error_position);
type_destroy(&t);
if (object_get_pointed_object(p_b_object))
{
//*b must be empty before copying to void* owner
struct type t = type_remove_pointer(p_b_type);
checked_empty(ctx, &t, object_get_pointed_object(p_b_object), error_position);
type_destroy(&t);
}

if (assigment_type == ASSIGMENT_TYPE_PARAMETER)
object_set_uninitialized(p_b_type, p_b_object);
else
object_set_moved(p_b_type, p_b_object);

}
return;
}
Expand All @@ -23389,7 +23400,16 @@ void object_assignment3(struct parser_ctx* ctx,
{
p_a_object->state = p_b_object->state;

checked_read_object(ctx, p_b_type, p_b_object, error_position, true);
struct type t = type_remove_pointer(p_a_type);

/*if the parameter points to out object, then we don�t need to check
argument pointed object.
*/
const bool checked_pointed_object_read = !type_is_out(&t);

checked_read_object(ctx, p_b_type, p_b_object, error_position, checked_pointed_object_read);

type_destroy(&t);

if (type_is_owner(p_a_type))
{
Expand All @@ -23398,6 +23418,19 @@ void object_assignment3(struct parser_ctx* ctx,
else
object_set_moved(p_b_type, p_b_object);
}
else
{
if (assigment_type == ASSIGMENT_TYPE_PARAMETER)
{
struct type t = type_remove_pointer(p_a_type);
if (!type_is_const(&t))
{
object_set_unknown(&t, object_get_pointed_object(p_b_object));
}
type_destroy(&t);
}
}


return;
}
Expand Down Expand Up @@ -23497,7 +23530,7 @@ void format_visit(struct format_visit_ctx* ctx);

//#pragma once

#define NEW_FLOW_ANALYSIS 1
//#define NEW_FLOW_ANALYSIS 1

/*
To be able to do static analysis with goto jump, we
Expand Down
49 changes: 41 additions & 8 deletions src/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,7 @@ void checked_empty(struct parser_ctx* ctx,
compiler_diagnostic_message(W_OWNERSHIP_FLOW_MOVED,
ctx,
position_token,
"object '%s' it not empty",
"object '%s' is not empty",
name);
}
}
Expand Down Expand Up @@ -1403,6 +1403,7 @@ void object_set_moved(struct type* p_type, struct object* p_object)
}
return;
}
return;
}

if (type_is_pointer(p_type))
Expand Down Expand Up @@ -1468,6 +1469,7 @@ void object_set_unknown(struct type* p_type, struct object* p_object)
}
return;
}
return;
}

if (type_is_pointer(p_type))
Expand All @@ -1484,7 +1486,8 @@ void object_set_unknown(struct type* p_type, struct object* p_object)
}
else
{
p_object->state = OBJECT_STATE_ZERO | OBJECT_STATE_NOT_ZERO;
if (!type_is_struct_or_union(p_type))
p_object->state = OBJECT_STATE_ZERO | OBJECT_STATE_NOT_ZERO;
}
}

Expand Down Expand Up @@ -1535,6 +1538,7 @@ void object_set_zero(struct type* p_type, struct object* p_object)
}
return;
}
return;
}

if (type_is_pointer(p_type))
Expand Down Expand Up @@ -2407,7 +2411,7 @@ void object_assignment3(struct parser_ctx* ctx,
error_position,
"assignment of possible null object '%s' to non-opt pointer", buffer);
#endif //nullchecks disabled for now
}
}

if (type_is_owner(p_a_type) && type_is_pointer(p_a_type))
{
Expand All @@ -2422,17 +2426,24 @@ void object_assignment3(struct parser_ctx* ctx,
}
}

/*copying to void * owner*/
if (type_is_void_ptr(p_a_type) && type_is_pointer(p_b_type))
{
if (type_is_owner(p_a_type) && object_get_pointed_object(p_b_object))
if (type_is_owner(p_a_type))
{
struct type t = type_remove_pointer(p_b_type);
checked_empty(ctx, &t, object_get_pointed_object(p_b_object), error_position);
type_destroy(&t);
if (object_get_pointed_object(p_b_object))
{
//*b must be empty before copying to void* owner
struct type t = type_remove_pointer(p_b_type);
checked_empty(ctx, &t, object_get_pointed_object(p_b_object), error_position);
type_destroy(&t);
}

if (assigment_type == ASSIGMENT_TYPE_PARAMETER)
object_set_uninitialized(p_b_type, p_b_object);
else
object_set_moved(p_b_type, p_b_object);

}
return;
}
Expand All @@ -2441,7 +2452,16 @@ void object_assignment3(struct parser_ctx* ctx,
{
p_a_object->state = p_b_object->state;

checked_read_object(ctx, p_b_type, p_b_object, error_position, true);
struct type t = type_remove_pointer(p_a_type);

/*if the parameter points to out object, then we don´t need to check
argument pointed object.
*/
const bool checked_pointed_object_read = !type_is_out(&t);

checked_read_object(ctx, p_b_type, p_b_object, error_position, checked_pointed_object_read);

type_destroy(&t);

if (type_is_owner(p_a_type))
{
Expand All @@ -2450,6 +2470,19 @@ void object_assignment3(struct parser_ctx* ctx,
else
object_set_moved(p_b_type, p_b_object);
}
else
{
if (assigment_type == ASSIGMENT_TYPE_PARAMETER)
{
struct type t = type_remove_pointer(p_a_type);
if (!type_is_const(&t))
{
object_set_unknown(&t, object_get_pointed_object(p_b_object));
}
type_destroy(&t);
}
}


return;
}
Expand Down
2 changes: 1 addition & 1 deletion vc/cprimev3.vcxproj.user
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerCommandArguments>file.c -fanalyzer -nullchecks</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>file.c -fanalyzer</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(ProjectDir)/../src/</LocalDebuggerWorkingDirectory>
</PropertyGroup>
Expand Down

0 comments on commit 2561775

Please sign in to comment.