Skip to content

Commit

Permalink
Remove unnecessary null pointer checks (fixes #34).
Browse files Browse the repository at this point in the history
  • Loading branch information
JusticeRage committed Aug 7, 2018
1 parent 7c0bc4c commit 76e5a8b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/writing-plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ External plugin skeleton:
extern "C"
{
PLUGIN_API IPlugin* create() { return new HelloWorldPlugin(); }
PLUGIN_API void destroy(IPlugin* p) { if (p) delete p; }
PLUGIN_API void destroy(IPlugin* p) { delete p; }
};

} //!namespace plugin
Expand Down
2 changes: 1 addition & 1 deletion plugins/plugin_authenticode/plugin_authenticode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class AuthenticodePlugin : public IPlugin
extern "C"
{
PLUGIN_API IPlugin* create() { return new AuthenticodePlugin(); }
PLUGIN_API void destroy(IPlugin* p) { if (p) delete p; }
PLUGIN_API void destroy(IPlugin* p) { delete p; }
};

// ----------------------------------------------------------------------------
Expand Down
16 changes: 8 additions & 8 deletions plugins/plugin_authenticode/plugin_authenticode_openssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void check_version_info(const mana::PE& pe, pResult res);
*
* @return A string containing the contents of the BIO.
*/
std::string bio_to_string(pBIO bio)
std::string bio_to_string(const pBIO& bio)

This comment has been minimized.

Copy link
@elfring

elfring Aug 8, 2018

I wonder how such a change could fit to the commit subject.
Would it have been safer to publish further adjustments in separate commits?

This comment has been minimized.

Copy link
@JusticeRage

JusticeRage Aug 8, 2018

Author Owner

Yes, you're right. These changes were not supposed to be staged for this commit, but I added them mistakenly.

{
BUF_MEM* buf = nullptr; // The memory pointed by this is freed with the BIO.
BIO_get_mem_ptr(bio.get(), &buf);
Expand Down Expand Up @@ -118,7 +118,7 @@ std::string get_CN(const std::string& x509_name)
* @param pPKCS7 p The PKCS7 object containing the digital signature.
* @param pResult res The result in which the names should be added.
*/
void add_certificate_information(pPKCS7 p, pResult res)
void add_certificate_information(const pPKCS7& p, const pResult& res)
{
STACK_OF(X509)* signers = PKCS7_get0_signers(p.get(), nullptr, 0);
if (signers == nullptr)
Expand Down Expand Up @@ -171,29 +171,29 @@ class OpenSSLAuthenticodePlugin : public IPlugin
pResult res = create_result();

auto certs = pe.get_certificates();
if (certs == nullptr || certs->size() == 0) // No authenticode signature.
if (certs == nullptr || certs->empty()) // No authenticode signature.
{
check_version_info(pe, res);
return res;
}

for (auto it = certs->begin() ; it != certs->end() ; ++it)
for (const auto& it : *certs)
{
// Disregard non-PKCS7 certificates. According to the spec, they are not
// supported by Windows.
if ((*it)->CertificateType != WIN_CERT_TYPE_PKCS_SIGNED_DATA) {
if (it->CertificateType != WIN_CERT_TYPE_PKCS_SIGNED_DATA) {
continue;
}

// Copy the certificate bytes into an OpenSSL BIO.
pBIO bio(BIO_new_mem_buf(&(*it)->Certificate[0], (*it)->Certificate.size()), BIO_free);
pBIO bio(BIO_new_mem_buf(&it->Certificate[0], it->Certificate.size()), BIO_free);
if (bio == nullptr)
{
PRINT_WARNING << "[plugin_authenticode] Could not initialize a BIO." << std::endl;
continue;
}

pPKCS7 p(d2i_PKCS7_bio(bio.get(), NULL), PKCS7_free);
pPKCS7 p(d2i_PKCS7_bio(bio.get(), nullptr), PKCS7_free);
if (p == nullptr)
{
PRINT_WARNING << "[plugin_authenticode] Error reading the PKCS7 certificate." << std::endl;
Expand All @@ -215,7 +215,7 @@ class OpenSSLAuthenticodePlugin : public IPlugin
extern "C"
{
PLUGIN_API IPlugin* create() { return new OpenSSLAuthenticodePlugin(); }
PLUGIN_API void destroy(IPlugin* p) { if (p) delete p; }
PLUGIN_API void destroy(IPlugin* p) { delete p; }
};

} //!namespace plugin
2 changes: 1 addition & 1 deletion plugins/plugin_virustotal/plugin_virustotal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ bool query_virus_total(const std::string& hash, const std::string& api_key, std:
extern "C"
{
PLUGIN_API IPlugin* create() { return new VirusTotalPlugin(); }
PLUGIN_API void destroy(IPlugin* p) { if (p) delete p; }
PLUGIN_API void destroy(IPlugin* p) { delete p; }
};

} // !namespace plugin
2 changes: 1 addition & 1 deletion plugins/plugins_yara.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace plugin
// Provide a destructor for the structure sent to Yara.
void delete_manape_module_data(manape_data* data)
{
if (data != nullptr && data->sections != nullptr) {
if (data != nullptr) {
free(data->sections);
}
delete data;
Expand Down

0 comments on commit 76e5a8b

Please sign in to comment.