Skip to content

Commit

Permalink
refactor: rename Scanner::timeout to Scanner::set_timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Mar 19, 2024
1 parent e344cd5 commit 644eab9
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions capi/include/yara-x.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ void yrx_scanner_destroy(struct YRX_SCANNER *scanner);
// after the designated timeout duration. However, in some cases, particularly
// with rules containing only a few patterns, the scanner could potentially
// continue running for a longer period than the specified timeout.
enum YRX_RESULT yrx_scanner_timeout(struct YRX_SCANNER *scanner,
uint64_t timeout);
enum YRX_RESULT yrx_scanner_set_timeout(struct YRX_SCANNER *scanner,
uint64_t timeout);

// Scans a data buffer.
//
Expand Down
4 changes: 2 additions & 2 deletions capi/src/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub unsafe extern "C" fn yrx_scanner_destroy(scanner: *mut YRX_SCANNER) {
/// with rules containing only a few patterns, the scanner could potentially
/// continue running for a longer period than the specified timeout.
#[no_mangle]
pub unsafe extern "C" fn yrx_scanner_timeout(
pub unsafe extern "C" fn yrx_scanner_set_timeout(
scanner: *mut YRX_SCANNER,
timeout: u64,
) -> YRX_RESULT {
Expand All @@ -61,7 +61,7 @@ pub unsafe extern "C" fn yrx_scanner_timeout(
}

let scanner = scanner.as_mut().unwrap();
scanner.inner.timeout(Duration::from_secs(timeout));
scanner.inner.set_timeout(Duration::from_secs(timeout));

YRX_RESULT::SUCCESS
}
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ pub fn exec_scan(args: &ArgMatches) -> anyhow::Result<()> {
let elapsed_time = Instant::elapsed(&start_time);

if let Some(timeout) = timeout.checked_sub(elapsed_time) {
scanner.timeout(timeout);
scanner.set_timeout(timeout);
} else {
return Err(Error::from(ScanError::Timeout));
}
Expand Down
2 changes: 1 addition & 1 deletion go/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewScanner(r *Rules) *Scanner {
// with rules containing only a few patterns, the scanner could potentially
// continue running for a longer period than the specified timeout.
func (s *Scanner) SetTimeout(timeout time.Duration) {
C.yrx_scanner_timeout(s.cScanner, C.uint64_t(math.Ceil(timeout.Seconds())))
C.yrx_scanner_set_timeout(s.cScanner, C.uint64_t(math.Ceil(timeout.Seconds())))
runtime.KeepAlive(s)
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/scanner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl<'r> Scanner<'r> {
/// in some cases, particularly with rules containing only a few patterns,
/// the scanner could potentially continue running for a longer period than
/// the specified timeout.
pub fn timeout(&mut self, timeout: Duration) -> &mut Self {
pub fn set_timeout(&mut self, timeout: Duration) -> &mut Self {
self.timeout = Some(timeout);
self
}
Expand Down
4 changes: 2 additions & 2 deletions py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ impl Scanner {
/// Sets a timeout for each scan.
///
/// After setting a timeout scans will abort after the specified `seconds`.
fn timeout(&mut self, seconds: u64) {
self.inner.timeout(Duration::from_secs(seconds));
fn set_timeout(&mut self, seconds: u64) {
self.inner.set_timeout(Duration::from_secs(seconds));
}

/// Sets a callback that is invoked every time a YARA rule calls the
Expand Down
3 changes: 2 additions & 1 deletion py/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest
import yara_x


def test_syntax_error():
compiler = yara_x.Compiler()
with pytest.raises(SyntaxError):
Expand Down Expand Up @@ -138,7 +139,7 @@ def test_scanner_timeout():
compiler.add_source(
'rule foo {condition: for all i in (0..10000000000) : ( true )}')
scanner = yara_x.Scanner(compiler.build())
scanner.timeout(1)
scanner.set_timeout(1)
with pytest.raises(Exception, match='timeout'):
scanner.scan(b'foobar')

Expand Down

0 comments on commit 644eab9

Please sign in to comment.