Skip to content

Add PR_7_c for c#8

Open
branedev2 wants to merge 1 commit intomainfrom
14jul_PR_7_c
Open

Add PR_7_c for c#8
branedev2 wants to merge 1 commit intomainfrom
14jul_PR_7_c

Conversation

@branedev2
Copy link
Copy Markdown
Owner

No description provided.

@amazon-q-developer
Copy link
Copy Markdown

⏳ I'm reviewing this pull request for security vulnerabilities and code quality issues. I'll provide an update when I'm done

Copy link
Copy Markdown

@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.

Summary of Changes

Hello @branedev2, 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 significantly expands the C language code examples within the repository, focusing on demonstrating a variety of SonarSource security and quality rules. The intent is to provide clear, actionable illustrations of secure coding patterns and common vulnerabilities, thereby aiding in the education of developers and the validation of static analysis tools.

Highlights

  • New C Code Examples for SonarSource Rules: This pull request introduces a substantial collection of C code examples, specifically designed to illustrate various SonarSource security and quality rules. Each rule is accompanied by both compliant and non-compliant code snippets.
  • Demonstration of Secure Coding Practices: The added examples cover a wide range of common C programming pitfalls and secure coding practices, including memory management (use-after-free, double-free), buffer overflows (strcpy, strcat, scanf), file access vulnerabilities (TOCTOU, chroot, temporary files), authentication, thread safety, and general code quality issues (uninitialized variables, unused assignments, switch statements).
  • Enhanced Static Analysis Testing: By providing clear, concise examples of both good and bad code patterns, these additions will be valuable for testing and validating static analysis tools, ensuring they correctly identify and flag potential issues in C projects.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 is currently in preview and 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 to provide feedback.

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.

@branedev2 branedev2 closed this Jul 14, 2025
@branedev2 branedev2 reopened this Jul 14, 2025
@amazon-q-developer
Copy link
Copy Markdown

⏳ I'm reviewing this pull request for security vulnerabilities and code quality issues. I'll provide an update when I'm done

char *cp = (char*)malloc(sizeof(char)*10); // memory is allocated
// all bytes in cp can be used here
free(cp); // memory is released
cp[9] = 0; // Noncompliant: memory is used after it was released
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Description: Accessing already freed memory can lead to critical issues like data corruption or unintended code execution, depending on how and when the memory is reused. This scenario resembles repurposing memory intended for release, resulting in unpredictable behavior and security vulnerabilities. To prevent such issues, practice careful memory management, ensuring proper allocation and deallocation procedures are followed, and avoid accessing memory after it has been freed. Learn more - https://owasp.org/www-community/vulnerabilities/Using_freed_memory

Severity: Critical

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The fix involves removing the use of freed memory and setting pointers to NULL after freeing to prevent accidental use. The second allocation is also properly freed before setting the pointer to NULL.

Suggested change
cp[9] = 0; // Noncompliant: memory is used after it was released
void freed_memory_should_not_be_used_non_complaint() // any function
{
char *cp = (char*)malloc(sizeof(char)*10); // memory is allocated
// all bytes in cp can be used here
free(cp); // memory is released
cp = NULL; // Set pointer to NULL after freeing
int *intArray = malloc(sizeof(int) *20); // memory is allocated
// elements of intArray can be written or read here
free(intArray); // Free the allocated memory
intArray = NULL; // Set pointer to NULL after freeing
}

{ // {fact rule=null-pointer-dereference@v1.0 defects=0}
char *p1 = NULL ;
if(p1 != NULL && *p1 == '\t') { // Compliant: *p1 cannot be evaluated if p1 is NULL
p1[2] = '\t';
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Description: Accessing already freed memory can lead to critical issues like data corruption or unintended code execution, depending on how and when the memory is reused. This scenario resembles repurposing memory intended for release, resulting in unpredictable behavior and security vulnerabilities. To prevent such issues, practice careful memory management, ensuring proper allocation and deallocation procedures are followed, and avoid accessing memory after it has been freed. Learn more - https://owasp.org/www-community/vulnerabilities/Using_freed_memory

Severity: Critical

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The vulnerability is addressed by removing the line p1[2] = '\t'; from the first if block, as it was attempting to access memory through a potentially null pointer.

Suggested change
p1[2] = '\t';
{ // {fact rule=null-pointer-dereference@v1.0 defects=0}
char *p1 = NULL ;
if(p1 != NULL && *p1 == '\t') { // Compliant: *p1 cannot be evaluated if p1 is NULL
// Removed: p1[2] = '\t';
}
// {/fact}

int *intArray = malloc(sizeof(int) *20); // memory is allocated
// elements of intArray can be written or read here
intArray=NULL; // memory is released
intArray[3] = 10; // Noncompliant: memory is used after it was released
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Description: Accessing already freed memory can lead to critical issues like data corruption or unintended code execution, depending on how and when the memory is reused. This scenario resembles repurposing memory intended for release, resulting in unpredictable behavior and security vulnerabilities. To prevent such issues, practice careful memory management, ensuring proper allocation and deallocation procedures are followed, and avoid accessing memory after it has been freed. Learn more - https://owasp.org/www-community/vulnerabilities/Using_freed_memory

Severity: Critical

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The fix involves freeing the allocated memory and then setting the pointers to NULL to prevent accidental use after free. This ensures that any subsequent attempts to access the freed memory through these pointers will result in a null pointer dereference, which is easier to detect and debug than use-after-free issues.

Suggested change
intArray[3] = 10; // Noncompliant: memory is used after it was released
char *cp = (char*)malloc(sizeof(char)*10); // memory is allocated
// all bytes in cp can be used here
free(cp); // memory is released
cp = NULL; // Set pointer to NULL after freeing
// {/fact}
// {fact rule=use-after-free@v1.0 defects=1}
int *intArray = malloc(sizeof(int) *20); // memory is allocated
// elements of intArray can be written or read here
free(intArray); // Free the allocated memory
intArray = NULL; // Set pointer to NULL after freeing
// {/fact}
}

int primes[] = { 1, 2, 3, 5, 7, 13, 17, 19};

// {fact rule=incorrect-use-of-sizeof@v1.0 defects=1}
size_t const primesSize2 = sizeof(primes + 1) / sizeof(int); // Noncompliant, type of primes + 1 is int *
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Description: Incorrect use of the sizeof operator can lead to critical issues such as improper memory allocation, resulting in program instability, crashes, or security vulnerabilities. To prevent these risks, ensure that sizeof is applied correctly, particularly when allocating memory dynamically. It is essential to use sizeof with the appropriate data type or expression to ensure proper memory management and avoid potential problems in your program. Learn more - https://cwe.mitre.org/data/definitions/467.html

Severity: Critical

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The fix involves using sizeof on the dereferenced pointer for dataSize, and using sizeof on the entire array divided by the size of a single element for arraySize and primesSize2. This ensures correct calculation of sizes for pointers and arrays.

Suggested change
size_t const primesSize2 = sizeof(primes + 1) / sizeof(int); // Noncompliant, type of primes + 1 is int *
void fun(int *data) {
size_t const dataSize = sizeof(*data); // Fixed: Use sizeof on the dereferenced pointer
int array[] = { 1, 2, 3, 5, 7, 13, 17, 19};
size_t const arraySize = sizeof(array) / sizeof(array[0]); // Fixed: Use sizeof on the entire array
int primes[] = { 1, 2, 3, 5, 7, 13, 17, 19};
size_t const primesSize2 = sizeof(primes) / sizeof(primes[0]); // Fixed: Use sizeof on the entire array
}


// {fact rule=missing-step-in-authentication@v1.0 defects=1}
int valid(pam_handle_t *pamh) {
if (pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK) != PAM_SUCCESS) { // Noncompliant
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Description: The function 'valid' only checks for authentication success but doesn't verify the account's validity. Add a call to pam_acct_mgmt() after successful authentication to verify the account's validity.

Severity: Critical

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The fix adds a call to pam_acct_mgmt() after successful authentication to verify the account's validity. This addresses the missing step in the authentication process by not only checking if the authentication succeeds but also verifying if the account is valid and allowed to access the system. The fix is incomplete as it assumes the pam_acct_mgmt() function is available, which may require including the appropriate PAM header file and linking against the PAM library.

Suggested change
if (pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK) != PAM_SUCCESS) { // Noncompliant
// {fact rule=missing-step-in-authentication@v1.0 defects=1}
int valid(pam_handle_t *pamh) {
if (pam_authenticate(pamh, PAM_DISALLOW_NULL_AUTHTOK) != PAM_SUCCESS) {
return -1;
}
if (pam_acct_mgmt(pamh, 0) != PAM_SUCCESS) {
return -1;
}

char *p2 = NULL ;
if(p2 != NULL) {
// ...
p2[2] = '\t'; // Compliant: p2 is known to be non-null
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Description: Accessing already freed memory can lead to critical issues like data corruption or unintended code execution, depending on how and when the memory is reused. This scenario resembles repurposing memory intended for release, resulting in unpredictable behavior and security vulnerabilities. To prevent such issues, practice careful memory management, ensuring proper allocation and deallocation procedures are followed, and avoid accessing memory after it has been freed. Learn more - https://owasp.org/www-community/vulnerabilities/Using_freed_memory

Severity: Critical

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The fix adds an additional null check for p2 immediately before accessing it, ensuring that p2 is still non-null at the point of access, even if it might have been modified between the initial check and the access point.

Suggested change
p2[2] = '\t'; // Compliant: p2 is known to be non-null
char *p2 = NULL ;
if(p2 != NULL) {
// ...
if (p2 != NULL) { // Additional check to ensure p2 is still non-null
p2[2] = '\t'; // Compliant: p2 is known to be non-null
}
}
// {/fact}
}


void fun(int *data) {
// {fact rule=incorrect-use-of-sizeof@v1.0 defects=1}
size_t const dataSize = sizeof data / sizeof(int); // Noncompliant, type of data is int *
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Description: Incorrect use of the sizeof operator can lead to critical issues such as improper memory allocation, resulting in program instability, crashes, or security vulnerabilities. To prevent these risks, ensure that sizeof is applied correctly, particularly when allocating memory dynamically. It is essential to use sizeof with the appropriate data type or expression to ensure proper memory management and avoid potential problems in your program. Learn more - https://cwe.mitre.org/data/definitions/467.html

Severity: Critical

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The fix involves passing the length of the data array as a parameter, using the correct sizeof expressions for array and primes, and removing the incorrect pointer arithmetic. This ensures proper size calculations for the arrays and avoids undefined behavior.

Suggested change
size_t const dataSize = sizeof data / sizeof(int); // Noncompliant, type of data is int *
void fun(int *data, size_t dataLength) {
size_t const dataSize = dataLength;
int array[] = { 1, 2, 3, 5, 7, 13, 17, 19};
size_t const arraySize = sizeof(array) / sizeof(array[0]);
int primes[] = { 1, 2, 3, 5, 7, 13, 17, 19};
size_t const primesSize2 = sizeof(primes) / sizeof(primes[0]);
}

int array[] = { 1, 2, 3, 5, 7, 13, 17, 19};

// {fact rule=incorrect-use-of-sizeof@v1.0 defects=1}
size_t const arraySize = sizeof(array + 1) / sizeof(int); // Noncompliant, type of array is int * too
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Description: Incorrect use of the sizeof operator can lead to critical issues such as improper memory allocation, resulting in program instability, crashes, or security vulnerabilities. To prevent these risks, ensure that sizeof is applied correctly, particularly when allocating memory dynamically. It is essential to use sizeof with the appropriate data type or expression to ensure proper memory management and avoid potential problems in your program. Learn more - https://cwe.mitre.org/data/definitions/467.html

Severity: Critical

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The fix addresses the incorrect use of sizeof by applying it directly to the array names (array and primes) instead of using pointer arithmetic. For the pointer parameter 'data', we use pointer dereferencing to get the correct size of a single element.

Suggested change
size_t const arraySize = sizeof(array + 1) / sizeof(int); // Noncompliant, type of array is int * too
void fun(int *data) {
size_t const dataSize = sizeof(data) / sizeof(*data); // Fixed: Use pointer dereferencing
int array[] = { 1, 2, 3, 5, 7, 13, 17, 19};
size_t const arraySize = sizeof(array) / sizeof(array[0]); // Fixed: Use array name without addition
int primes[] = { 1, 2, 3, 5, 7, 13, 17, 19};
size_t const primesSize2 = sizeof(primes) / sizeof(primes[0]); // Fixed: Use array name without addition
}

@amazon-q-developer
Copy link
Copy Markdown

✅ I finished the code review, and left comments with the issues I found. I will now generate code fix suggestions.

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