@@ -15,38 +15,50 @@ public ClamEngine ()
1515 throw new Exception ( "Expected CL_SUCCESS, got " + ret ) ;
1616
1717 engine = ClamBindings . cl_engine_new ( ) ;
18-
19- string dbDir = Marshal . PtrToStringAnsi ( ClamBindings . cl_retdbdir ( ) ) ;
20- uint signo = 0 ;
21-
22- ret = ClamBindings . cl_load ( dbDir , engine , ref signo , ( uint ) ClamScanOptions . CL_SCAN_STDOPT ) ;
23-
24- if ( ret != ClamReturnCode . CL_SUCCESS )
25- throw new Exception ( "Expected CL_SUCCESS, got " + ret ) ;
26-
27- ret = ( ClamReturnCode ) ClamBindings . cl_engine_compile ( engine ) ;
28-
29- if ( ret != ClamReturnCode . CL_SUCCESS )
30- throw new Exception ( "Expected CL_SUCCESS, got " + ret ) ;
18+
19+ try
20+ {
21+ string dbDir = Marshal . PtrToStringAnsi ( ClamBindings . cl_retdbdir ( ) ) ;
22+ uint signo = 0 ;
23+
24+ ret = ClamBindings . cl_load ( dbDir , engine , ref signo , ( uint ) ClamScanOptions . CL_SCAN_STDOPT ) ;
25+
26+ if ( ret != ClamReturnCode . CL_SUCCESS )
27+ throw new Exception ( "Expected CL_SUCCESS, got " + ret ) ;
28+
29+ ret = ( ClamReturnCode ) ClamBindings . cl_engine_compile ( engine ) ;
30+
31+ if ( ret != ClamReturnCode . CL_SUCCESS )
32+ throw new Exception ( "Expected CL_SUCCESS, got " + ret ) ;
33+ }
34+ catch
35+ {
36+ ret = ClamBindings . cl_engine_free ( engine ) ;
37+
38+ if ( ret != ClamReturnCode . CL_SUCCESS )
39+ Console . Error . WriteLine ( "Freeing allocated engine failed" ) ;
40+ }
3141 }
3242
3343 public ClamResult ScanFile ( string filepath , uint options = ( uint ) ClamScanOptions . CL_SCAN_STDOPT )
3444 {
3545 ulong scanned = 0 ;
3646 IntPtr vname = ( IntPtr ) null ;
3747 ClamReturnCode ret = ClamBindings . cl_scanfile ( filepath , ref vname , ref scanned , engine , options ) ;
38-
39- if ( ret == ClamReturnCode . CL_VIRUS ) {
40- string virus = Marshal . PtrToStringAnsi ( vname ) ;
41-
42- ClamResult result = new ClamResult ( ) ;
48+
49+ if ( ret == ClamReturnCode . CL_VIRUS )
50+ {
51+ string virus = Marshal . PtrToStringAnsi ( vname ) ;
52+
53+ ClamResult result = new ClamResult ( ) ;
4354 result . ReturnCode = ret ;
4455 result . VirusName = virus ;
4556 result . FullPath = filepath ;
46-
57+
4758 return result ;
48- } else if ( ret == ClamReturnCode . CL_CLEAN )
49- return null ;
59+ }
60+ else if ( ret == ClamReturnCode . CL_CLEAN )
61+ return new ClamResult ( ) { ReturnCode = ret } ;
5062 else
5163 throw new Exception ( "Expected either CL_CLEAN or CL_VIRUS, got: " + ret ) ;
5264 }
@@ -56,7 +68,7 @@ public void Dispose()
5668 ClamReturnCode ret = ClamBindings . cl_engine_free ( engine ) ;
5769
5870 if ( ret != ClamReturnCode . CL_SUCCESS )
59- throw new Exception ( "Expected CL_SUCCESS, got " + ret ) ;
71+ Console . Error . WriteLine ( "Freeing allocated engine failed" ) ;
6072 }
6173 }
6274}
0 commit comments