Skip to content

save and load operations#7

Open
Raquezin wants to merge 1 commit intomainfrom
load_save
Open

save and load operations#7
Raquezin wants to merge 1 commit intomainfrom
load_save

Conversation

@Raquezin
Copy link
Owner

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Raquezin, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces essential functionality for saving and loading matrix data to and from binary files. This enhancement allows for the persistence of matrix states, enabling users to store complex matrix structures and retrieve them later, which is crucial for applications requiring data retention or inter-process data exchange. The changes include updates to error handling and the core matrix library to support these new operations.

Highlights

  • Error Handling: A new error code, ERR_FILE_NOT_FOUND, has been added to include/errores.h to specifically handle cases where a file cannot be opened for reading or writing.
  • Matrix Persistence API: Two new functions, save_matrix and load_matrix, have been declared in include/matrix.h. These functions provide an interface for serializing and deserializing matrix data to and from binary files.
  • Binary File Operations: The src/matrix.c file now includes implementations for save_matrix and load_matrix. These functions use binary file I/O (fwrite and fread) to store and retrieve matrix dimensions (rows, columns) and the matrix data itself.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces functionality to save and load matrices to/from binary files. While this is a valuable addition, the current implementation contains several critical vulnerabilities related to file handling. Specifically, the functions lack proper error checking for I/O operations (fread and fwrite) and memory allocation, which can lead to silent data corruption, crashes from null pointer dereferences, and incorrect error reporting. I've provided detailed comments and suggestions in src/matrix.c to address these critical issues and make the file operations robust and safe.

Comment on lines +39 to +49
ErrorCode save_matrix(const Matrix a, const char* filename) {
FILE* f = fopen(filename, "wb");
if (!f) return ERR_FILE_NOT_FOUND;

fwrite(&a.row, sizeof(int), 1, f);
fwrite(&a.col, sizeof(int), 1, f);
fwrite(a.data, sizeof(float), a.row * a.col, f);

fclose(f);
return ERR_NONE;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The current implementation does not check the return value of fwrite. If a write operation fails (e.g., disk is full), the function will report success (ERR_NONE), but the resulting file will be corrupted. This can lead to silent data loss. All I/O operations should have their results checked. It would also be beneficial to add a more specific error code, like ERR_FILE_WRITE, to errores.h for this case.

ErrorCode save_matrix(const Matrix a, const char* filename) {
    FILE* f = fopen(filename, "wb");
    if (!f) {
        return ERR_FILE_NOT_FOUND;
    }

    if (fwrite(&a.row, sizeof(int), 1, f) != 1) {
        fclose(f);
        return ERR_FILE_NOT_FOUND;
    }
    if (fwrite(&a.col, sizeof(int), 1, f) != 1) {
        fclose(f);
        return ERR_FILE_NOT_FOUND;
    }

    size_t elements = (size_t)a.row * a.col;
    if (elements > 0 && fwrite(a.data, sizeof(float), elements, f) != elements) {
        fclose(f);
        return ERR_FILE_NOT_FOUND;
    }

    fclose(f);
    return ERR_NONE;
}

Comment on lines +51 to +66
Matrix load_matrix(const char* filename, ErrorCode* err) {
FILE* f = fopen(filename, "rb");
if (!f) {
*err = ERR_NONE;
return default_matrix;
}

int rows, cols;
fread(&rows, sizeof(int), 1, f);
fread(&cols, sizeof(int), 1, f);
Matrix m = create_matrix(rows, cols, 0.0f);
fread(m.data, sizeof(float), rows * cols, f);
fclose(f);
*err = ERR_NONE;
return m;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The implementation of load_matrix has several critical flaws that can lead to incorrect behavior, silent failures, and crashes:

  1. Incorrect Error on fopen Failure: If the file cannot be opened, *err is set to ERR_NONE, misleading the caller. It should be ERR_FILE_NOT_FOUND.
  2. Unchecked fread Calls: The return values of fread are ignored. If the file is corrupted or incomplete, the function may proceed with garbage data, leading to undefined behavior.
  3. Unhandled create_matrix Failure: If create_matrix fails (e.g., due to invalid dimensions or memory allocation failure), it returns a matrix with a NULL data pointer. The subsequent fread call will then cause a segmentation fault.
    The function should be rewritten to handle these error conditions robustly. It would also be beneficial to add more specific error codes for file read and memory allocation errors to errores.h.
Matrix load_matrix(const char* filename, ErrorCode* err) {
    FILE* f = fopen(filename, "rb");
    if (!f) {
        *err = ERR_FILE_NOT_FOUND;
        return default_matrix;
    }

    int rows, cols;
    if (fread(&rows, sizeof(int), 1, f) != 1) {
        fclose(f);
        *err = ERR_FILE_NOT_FOUND;
        return default_matrix;
    }
    if (fread(&cols, sizeof(int), 1, f) != 1) {
        fclose(f);
        *err = ERR_FILE_NOT_FOUND;
        return default_matrix;
    }

    Matrix m = create_matrix(rows, cols, 0.0f);
    if (m.data == NULL) { // Check if create_matrix failed
        fclose(f);
        *err = ERR_FILE_NOT_FOUND; // A more specific memory error would be better
        return default_matrix;
    }

    size_t elements = (size_t)rows * cols;
    if (elements > 0 && fread(m.data, sizeof(float), elements, f) != elements) {
        fclose(f);
        *err = ERR_FILE_NOT_FOUND;
        return default_matrix;
    }

    fclose(f);
    *err = ERR_NONE;
    return m;
}

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.

1 participant