Skip to content

Commit

Permalink
Don't allow negative values for OS.delay_usec()/OS.delay_msec()
Browse files Browse the repository at this point in the history
This closes #46190.

(cherry picked from commit 76f1f9b)
  • Loading branch information
Calinou authored and akien-mga committed Feb 19, 2021
1 parent 2fb221e commit 9f236d4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
14 changes: 10 additions & 4 deletions core/bind/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,13 +942,19 @@ uint64_t _OS::get_system_time_msecs() const {
return OS::get_singleton()->get_system_time_msecs();
}

void _OS::delay_usec(uint32_t p_usec) const {

/** This method uses a signed argument for better error reporting as it's used from the scripting API. */
void _OS::delay_usec(int p_usec) const {
ERR_FAIL_COND_MSG(
p_usec < 0,
vformat("Can't sleep for %d microseconds. The delay provided must be greater than or equal to 0 microseconds.", p_usec));
OS::get_singleton()->delay_usec(p_usec);
}

void _OS::delay_msec(uint32_t p_msec) const {

/** This method uses a signed argument for better error reporting as it's used from the scripting API. */
void _OS::delay_msec(int p_msec) const {
ERR_FAIL_COND_MSG(
p_msec < 0,
vformat("Can't sleep for %d milliseconds. The delay provided must be greater than or equal to 0 milliseconds.", p_msec));
OS::get_singleton()->delay_usec(int64_t(p_msec) * 1000);
}

Expand Down
4 changes: 2 additions & 2 deletions core/bind/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ class _OS : public Object {
uint64_t get_static_memory_peak_usage() const;
uint64_t get_dynamic_memory_usage() const;

void delay_usec(uint32_t p_usec) const;
void delay_msec(uint32_t p_msec) const;
void delay_usec(int p_usec) const;
void delay_msec(int p_msec) const;
uint32_t get_ticks_msec() const;
uint64_t get_ticks_usec() const;
uint32_t get_splash_tick_msec() const;
Expand Down
4 changes: 2 additions & 2 deletions doc/classes/OS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<argument index="0" name="msec" type="int">
</argument>
<description>
Delay execution of the current thread by [code]msec[/code] milliseconds.
Delay execution of the current thread by [code]msec[/code] milliseconds. [code]usec[/code] must be greater than or equal to [code]0[/code]. Otherwise, [method delay_msec] will do nothing and will print an error message.
</description>
</method>
<method name="delay_usec" qualifiers="const">
Expand All @@ -65,7 +65,7 @@
<argument index="0" name="usec" type="int">
</argument>
<description>
Delay execution of the current thread by [code]usec[/code] microseconds.
Delay execution of the current thread by [code]usec[/code] microseconds. [code]usec[/code] must be greater than or equal to [code]0[/code]. Otherwise, [method delay_usec] will do nothing and will print an error message.
</description>
</method>
<method name="dump_memory_to_file">
Expand Down

0 comments on commit 9f236d4

Please sign in to comment.