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

Exceptions + mmap + groth16_prover_zkey_file #10

Merged
merged 20 commits into from
Feb 6, 2024

Conversation

OBrezhniev
Copy link
Member

No description provided.

if (type != _type) {
munmap(addr, size);
close(fd);
throw new std::invalid_argument("Invalid file type. It should be " + _type + " and it is " + type + " filename: " + fileName);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In modern C++, it is not recommended to throw or catch exceptions by pointer; instead, use references. I would recommend removing the new keyword.

Furthermore, from what I observe, we do not free the memory allocated for exceptions caught by a pointer. This results in a memory leak.

if (version > maxVersion) {
munmap(addr, size);
close(fd);
throw new std::invalid_argument("Invalid version. It should be <=" + std::to_string(maxVersion) + " and it is " + std::to_string(version));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same for exception pointer


size = sb.st_size;

addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy recommends using C++ native nullptr variable instead of C-style NULL.

@@ -8,8 +12,70 @@

namespace BinFileUtils {

BinFile::BinFile(std::string fileName, std::string _type, uint32_t maxVersion) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy recommends using const std::string& fileName and const std::string& _type types.

src/prover.h Outdated
char *proof_buffer, unsigned long *proof_size,
char *public_buffer, unsigned long *public_size,
char *error_msg, unsigned long error_msg_maxsize);
int groth16_prover(const void *zkey_buffer, unsigned long zkey_size,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is a C header file (not C++), functions are declared in the traditional manner, such as

int
groth16_prover(...

his approach makes it easier to differentiate between function declarations and function usage using old-school grep-like utilities. Sure we do not obligated to support such style, but for me it looks more natural 🤷

FileLoader fileLoader(filename);
// FileLoader fileLoader(filename);
//
// return std::unique_ptr<BinFile>(new BinFile(fileLoader.dataBuffer(), fileLoader.dataSize(), type, maxVersion));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you forget to remove those lines?

close(fd);
} else {
free(addr);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this class is not safe for copy. I would delete default copy and assignment constructors to prevent these operations.

BinFile(const BinFile&) = delete;
BinFile& operator=(const BinFile&) = delete;

@OBrezhniev OBrezhniev merged commit 97c309c into main Feb 6, 2024
4 checks passed
@OBrezhniev OBrezhniev deleted the feature/exceptions_groth16_prover_zkey_file branch February 6, 2024 11:41
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.

2 participants