Skip to content

Commit

Permalink
Fix segfault in Character::clearBuffs() (#573)
Browse files Browse the repository at this point in the history
* Fix segfault in Character::clearBuffs()

This escript function was segfaulting every time it was
called because the implementation was deleting items
from a map while iterating through it.

* Add test for EScript character buff functions.

* Update core changelog.

* Update corechanges.xml
  • Loading branch information
brndd committed Dec 6, 2023
1 parent 42f15b8 commit ef624b3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
7 changes: 6 additions & 1 deletion docs/docs.polserver.com/pol100/corechanges.xml
Expand Up @@ -2,9 +2,14 @@
<ESCRIPT>
<header>
<topic>Latest Core Changes</topic>
<datemodified>12-03-2023</datemodified>
<datemodified>12-06-2023</datemodified>
</header>
<version name="POL100.1.0">
<entry>
<date>12-06-2023</date>
<author>Brndd:</author>
<change type="Fixed">crash due to character.clearBuffs()</change>
</entry>
<entry>
<date>12-03-2023</date>
<author>Brndd:</author>
Expand Down
2 changes: 2 additions & 0 deletions pol-core/doc/core-changes.txt
@@ -1,4 +1,6 @@
-- POL100.1.0 --
12-06-2023 Brndd:
Fixed: crash due to character.clearBuffs()
12-03-2023 Brndd:
Added: GetStandingLayers() now has a "includeitems" parameter which allows including or excluding items from the search (default: enabled).
Fixed: GetStandingLayers() now returns the height of results correctly.
Expand Down
8 changes: 7 additions & 1 deletion pol-core/pol/mobile/charactr.cpp
Expand Up @@ -4257,7 +4257,13 @@ bool Character::delBuff( u16 icon )
void Character::clearBuffs()
{
for ( auto it = buffs_.begin(); it != buffs_.end(); ++it )
delBuff( it->first );
{
if ( client != nullptr )
{
send_buff_message( this, it->first, false );
}
}
buffs_.clear();
}

/**
Expand Down
36 changes: 36 additions & 0 deletions testsuite/pol/testpkgs/char/test_chr.src
Expand Up @@ -89,3 +89,39 @@ exported function create_in_backpack_loc()
endif
return 1;
endfunction

exported function test_buffs()
var err;
err := char.addBuff(1029, 30, 1075814, 1075815, CAscZ("1234 5678"));
if (err == error)
return ret_error($"Could not add buff: {err}");
endif
err := char.delBuff(1029);
if (err == error)
return ret_error($"Could not delete buff: {err}");
endif

//Try clearing buffs
err := char.addBuff(1029, 30, 1075814, 1075815, CAscZ("1234 5678"));
print($"addBuff: {err}");
if (err == error)
return ret_error($"Could not add buff: {err}");
endif
err := char.clearBuffs();
if (err == error)
return ret_error($"Could not clear buffs: {err}");
endif

//Try deleting an inexistent buff, should fail
err := char.delBuff(1029);
if (err != error)
return ret_error($"Deleting an inexistent buff didn't cause an error: {err}");
endif

//Try clearing buffs when there are none
err := char.clearBuffs();
if (err == error)
return ret_error($"Could not clear buffs: {err}");
endif
return 1;
endfunction

0 comments on commit ef624b3

Please sign in to comment.