Skip to content

Catch processing exceptions and add to list#690

Merged
kristelmerilain merged 1 commit intomasterfrom
exception
Aug 22, 2025
Merged

Catch processing exceptions and add to list#690
kristelmerilain merged 1 commit intomasterfrom
exception

Conversation

@metsma
Copy link
Copy Markdown
Contributor

@metsma metsma commented Aug 21, 2025

IB-8568

Signed-off-by: Raul Metsma raul@metsma.ee

IB-8568

Signed-off-by: Raul Metsma <raul@metsma.ee>
{
for(auto data = sdop/"DataObjectFormat"; data; data++)
cb_doc = bdoc;
cb_exception = &exception;

Check warning

Code scanning / CodeQL

Local variable address stored in non-local memory Warning

A stack address (
source
) may be assigned to a non-local variable.

Copilot Autofix

AI 8 months ago

To fix this problem, we need to ensure that the address of a local variable is not stored in a non-local variable that may outlive the local variable's scope. The best way to do this is to allocate the exception object on the heap, so its lifetime is not tied to the function's stack frame. This can be done by replacing the local stack allocation of Exception exception(...) with a heap allocation (auto exception = std::make_unique<Exception>(...)), and then assigning cb_exception to point to this heap-allocated object. After the function is done, we must ensure the memory is properly released, either by deleting the object or letting a smart pointer handle it. Since the rest of the code expects an Exception& or Exception*, we may need to dereference or pass the pointer as needed.

Steps:

  • Change the declaration of exception from a stack variable to a heap-allocated one (using new or std::unique_ptr).
  • Assign cb_exception to point to this heap-allocated object.
  • After use, reset cb_exception and ensure the heap memory is released (if not using a smart pointer, call delete).
  • Update all uses of exception in this function to use the pointer (i.e., exception-> instead of exception.).
  • If the rest of the code expects a reference, dereference the pointer as needed.

Required changes:

  • In src/SignatureXAdES_B.cpp, lines around the declaration and use of exception and the assignment to cb_exception.
  • No new imports are needed if using new/delete. If using std::unique_ptr, include <memory>.

Suggested changeset 1
src/SignatureXAdES_B.cpp

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/SignatureXAdES_B.cpp b/src/SignatureXAdES_B.cpp
--- a/src/SignatureXAdES_B.cpp
+++ b/src/SignatureXAdES_B.cpp
@@ -407,7 +407,7 @@
     DEBUG("SignatureXAdES_B::validate(%s)", policy.c_str());
     // A "master" exception containing all problems (causes) with this signature.
     // It'll be only thrown in case we have a reason (cause).
-    Exception exception(EXCEPTION_PARAMS("Signature validation"));
+    auto exception = new Exception(EXCEPTION_PARAMS("Signature validation"));
 
     try {
         if(!Exception::hasWarningIgnore(Exception::SignatureDigestWeak) &&
@@ -415,7 +415,7 @@
         {
             Exception e(EXCEPTION_PARAMS("Signature digest weak"));
             e.setCode(Exception::SignatureDigestWeak);
-            exception.addCause(e);
+            exception->addCause(e);
         }
 
         if(profile().find(ASiC_E::ASIC_TM_PROFILE) != string::npos)
@@ -451,12 +451,12 @@
         }
 
         cb_doc = bdoc;
-        cb_exception = &exception;
-        bool result = XMLDocument::verifySignature(signature, &exception);
+        cb_exception = exception;
+        bool result = XMLDocument::verifySignature(signature, exception);
         cb_doc = {};
         cb_exception = {};
         if(!result)
-            EXCEPTION_ADD(exception, "Failed to validate signature");
+            EXCEPTION_ADD((*exception), "Failed to validate signature");
 
         auto sp = qualifyingProperties()/"SignedProperties";
         auto sdop = sp/"SignedDataObjectProperties";
@@ -556,6 +553,8 @@
 
     if(!exception.causes().empty())
         throw exception;
+    }
+    delete exception;
 }
 
 vector<unsigned char> SignatureXAdES_B::dataToSign() const
EOF
@@ -407,7 +407,7 @@
DEBUG("SignatureXAdES_B::validate(%s)", policy.c_str());
// A "master" exception containing all problems (causes) with this signature.
// It'll be only thrown in case we have a reason (cause).
Exception exception(EXCEPTION_PARAMS("Signature validation"));
auto exception = new Exception(EXCEPTION_PARAMS("Signature validation"));

try {
if(!Exception::hasWarningIgnore(Exception::SignatureDigestWeak) &&
@@ -415,7 +415,7 @@
{
Exception e(EXCEPTION_PARAMS("Signature digest weak"));
e.setCode(Exception::SignatureDigestWeak);
exception.addCause(e);
exception->addCause(e);
}

if(profile().find(ASiC_E::ASIC_TM_PROFILE) != string::npos)
@@ -451,12 +451,12 @@
}

cb_doc = bdoc;
cb_exception = &exception;
bool result = XMLDocument::verifySignature(signature, &exception);
cb_exception = exception;
bool result = XMLDocument::verifySignature(signature, exception);
cb_doc = {};
cb_exception = {};
if(!result)
EXCEPTION_ADD(exception, "Failed to validate signature");
EXCEPTION_ADD((*exception), "Failed to validate signature");

auto sp = qualifyingProperties()/"SignedProperties";
auto sdop = sp/"SignedDataObjectProperties";
@@ -556,6 +553,8 @@

if(!exception.causes().empty())
throw exception;
}
delete exception;
}

vector<unsigned char> SignatureXAdES_B::dataToSign() const
Copilot is powered by AI and may make mistakes. Always verify output.
@kristelmerilain kristelmerilain merged commit 9491bcc into master Aug 22, 2025
44 checks passed
@metsma metsma deleted the exception branch August 22, 2025 05:29
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

Successfully merging this pull request may close these issues.

3 participants