Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directly raising exception from 'Exception' instance #321

Open
openrefactory opened this issue May 19, 2022 · 0 comments
Open

Directly raising exception from 'Exception' instance #321

openrefactory opened this issue May 19, 2022 · 0 comments

Comments

@openrefactory
Copy link

OpenRefactory introduces fixers, that automatically fix safety, security, reliability and compliance issues. We ran OpenRefactory's Intelligent Code Repair (iCR) analysis engine on your code. We are reporting a few sample fixes that were generated.

Throwing overly broad exceptions promotes complex error handling code that is more likely to contain security vulnerabilities. It will become challenging to catch only specific types of exceptions. The best practice is to catch only exceptions which require specific handling.

Raising Exception in a function will lead to having an except Exception and the only way to differentiate between different exceptions is by comparing the exception messages. Moreover, one could forget to re-raise some exceptions which are unintentionally caught.

OpenRefactory’s Intelligent Code Repair (iCR) for Python, identified four such cases. The diffs are the following:

--- /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/sync/action.py
+++ /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/sync/action.py
@@ -241,6 +241,8 @@
             except OSError:
                 pass
         if not os.path.isdir(parent_dir):
+            # OpenRefactory Warning: Raising 'Exception' and 'BaseException' directly will have a negative impact on any code trying to catch these exceptions.
+            # Raise a more specific built-in exception or, create a custom one.
             raise Exception('could not create directory %s' % (parent_dir,))
 
     def do_action(self, bucket, reporter):
--- /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/v1/download_dest.py
+++ /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/v1/download_dest.py
@@ -175,6 +175,8 @@
 
     def get_bytes_written(self):
         if self.bytes_written is None:
+            # OpenRefactory Warning: Raising 'Exception' and 'BaseException' directly will have a negative impact on any code trying to catch these exceptions.
+            # Raise a more specific built-in exception or, create a custom one.
             raise Exception('data not written yet')
         return self.bytes_written
--- /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/v1/sync/folder.py
+++ /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/v1/sync/folder.py
@@ -22,6 +22,8 @@
         try:
             return func(*a, **kw)
         except exception.NotADirectory as ex:
+            # OpenRefactory Warning: Raising 'Exception' and 'BaseException' directly will have a negative impact on any code trying to catch these exceptions.
+            # Raise a more specific built-in exception or, create a custom one.
             raise Exception('%s is not a directory' % (ex.path,))
         except exception.UnableToCreateDirectory as ex:
             raise Exception('unable to create directory %s' % (ex.path,))
--- /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/v1/sync/folder.py
+++ /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/v1/sync/folder.py
@@ -24,6 +24,8 @@
         except exception.NotADirectory as ex:
             raise Exception('%s is not a directory' % (ex.path,))
         except exception.UnableToCreateDirectory as ex:
+            # OpenRefactory Warning: Raising 'Exception' and 'BaseException' directly will have a negative impact on any code trying to catch these exceptions.
+            # Raise a more specific built-in exception or, create a custom one.
             raise Exception('unable to create directory %s' % (ex.path,))
         except exception.EmptyDirectory as ex:
             raise exception.CommandError(

Reference: CWE-397: Declaration of Throws for Generic Exception


Reported by OpenRefactory’s Intelligent Code Repair (iCR) for Python v1.0. More info at: https://www.openrefactory.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant