Skip to content

Fix undefined behavior in dnscache.c resolveAddr#177

Open
rgerhards wants to merge 3 commits intomainfrom
fix-dnscache-tolower-ub-16300544792167519701
Open

Fix undefined behavior in dnscache.c resolveAddr#177
rgerhards wants to merge 3 commits intomainfrom
fix-dnscache-tolower-ub-16300544792167519701

Conversation

@rgerhards
Copy link
Owner

Fixed undefined behavior in runtime/dnscache.c where tolower() was called with a signed char, potentially leading to UB with negative values. Cast the argument to unsigned char.


PR created automatically by Jules for task 16300544792167519701 started by @rgerhards

When processing a domain name that contains characters with the high bit set
(e.g., non-ASCII characters or malicious input), passing a signed `char` to
`tolower()` results in Undefined Behavior if the value is negative.

This patch fixes the issue by casting the argument to `unsigned char`, ensuring
the value is within the valid range (0-255) expected by `tolower()`.

This addresses a potential security vulnerability (CERT STR37-C).

Co-authored-by: rgerhards <1482123+rgerhards@users.noreply.github.com>
@google-labs-jules
Copy link

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@gemini-code-assist
Copy link

Summary of Changes

Hello @rgerhards, 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 addresses a critical issue in the dnscache.c module by resolving undefined behavior that could occur when calling tolower() with signed char values. The change ensures robust character conversion, enhancing the stability and predictability of the DNS caching mechanism.

Highlights

  • Undefined Behavior Fix: Corrected a potential undefined behavior in runtime/dnscache.c by explicitly casting the argument to tolower() to unsigned char, preventing issues with negative char values.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • runtime/dnscache.c
    • Fixed undefined behavior by casting char to unsigned char before passing it to tolower().
Activity
  • No human activity has been recorded on this pull request yet.
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

@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 correctly addresses a potential undefined behavior in runtime/dnscache.c. By casting the char argument of tolower() to unsigned char, it ensures the function behaves correctly even on platforms where char is signed and for character values that would be negative. This is a crucial fix for portability and correctness. The change is minimal, targeted, and well-explained in the pull request description. I have no further suggestions for improvement on this change.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 1 file

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="runtime/dnscache.c">

<violation number="1" location="runtime/dnscache.c:313">
P2: Rule violated: **Code Quality Guard**

**SLOP_SCORE: High** — This patch fixes `tolower()` UB only in `dnscache.c` but leaves the identical bug unfixed in 4 other `runtime/` files (`net.c:1171`, `tcpsrv.c:752`, `srutils.c:597`, `msg.c:4097`). The `(int)` casts used in some of those files are also insufficient to prevent UB with negative `char` values — only `(unsigned char)` is correct. An AI-generated patch that claims to fix a class of UB but addresses a single instance is incomplete and sloppy.

| Metric | Score |
|---|---|
| AI_PROBABILITY | 95% |
| POLICY_COMPLIANCE | 60% |
| SLOP_SCORE | 70% |</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

fqdnLen = strlen(fqdnBuf);
prop.CreateStringProp(&etry->fqdn, (uchar *)fqdnBuf, fqdnLen);
for (i = 0; i < fqdnLen; ++i) fqdnBuf[i] = tolower(fqdnBuf[i]);
for (i = 0; i < fqdnLen; ++i) fqdnBuf[i] = tolower((unsigned char)fqdnBuf[i]);
Copy link

@cubic-dev-ai cubic-dev-ai bot Feb 15, 2026

Choose a reason for hiding this comment

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

P2: Rule violated: Code Quality Guard

SLOP_SCORE: High — This patch fixes tolower() UB only in dnscache.c but leaves the identical bug unfixed in 4 other runtime/ files (net.c:1171, tcpsrv.c:752, srutils.c:597, msg.c:4097). The (int) casts used in some of those files are also insufficient to prevent UB with negative char values — only (unsigned char) is correct. An AI-generated patch that claims to fix a class of UB but addresses a single instance is incomplete and sloppy.

Metric Score
AI_PROBABILITY 95%
POLICY_COMPLIANCE 60%
SLOP_SCORE 70%
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At runtime/dnscache.c, line 313:

<comment>**SLOP_SCORE: High** — This patch fixes `tolower()` UB only in `dnscache.c` but leaves the identical bug unfixed in 4 other `runtime/` files (`net.c:1171`, `tcpsrv.c:752`, `srutils.c:597`, `msg.c:4097`). The `(int)` casts used in some of those files are also insufficient to prevent UB with negative `char` values — only `(unsigned char)` is correct. An AI-generated patch that claims to fix a class of UB but addresses a single instance is incomplete and sloppy.

| Metric | Score |
|---|---|
| AI_PROBABILITY | 95% |
| POLICY_COMPLIANCE | 60% |
| SLOP_SCORE | 70% |</comment>

<file context>
@@ -310,7 +310,7 @@ static rsRetVal ATTR_NONNULL() resolveAddr(struct sockaddr_storage *addr, dnscac
                 fqdnLen = strlen(fqdnBuf);
                 prop.CreateStringProp(&etry->fqdn, (uchar *)fqdnBuf, fqdnLen);
-                for (i = 0; i < fqdnLen; ++i) fqdnBuf[i] = tolower(fqdnBuf[i]);
+                for (i = 0; i < fqdnLen; ++i) fqdnBuf[i] = tolower((unsigned char)fqdnBuf[i]);
                 prop.CreateStringProp(&etry->fqdnLowerCase, (uchar *)fqdnBuf, fqdnLen);
             }
</file context>
Fix with Cubic

google-labs-jules bot and others added 2 commits February 16, 2026 08:24
This patch fixes potential Undefined Behavior when passing signed `char`
values to `isdigit()` and `isspace()`.

In `plugins/imptcp/imptcp.c`, signed chars from the input buffer were
passed to `isdigit()` without casting, which can lead to UB if the input
contains bytes with the high bit set (negative values).

In `tools/omfile.c`, signed chars from the filename string were passed to
`isspace()` without casting, which is similarly unsafe for non-ASCII
filenames.

Both instances are now fixed by casting the argument to `(unsigned char)`
before passing it to the `ctype` functions, as required by the C standard
(CERT STR37-C).

Co-authored-by: rgerhards <1482123+rgerhards@users.noreply.github.com>
This patch makes the dependency downloading in `tests/diag.sh` more robust
by adding a `download_and_verify_tgz` helper function.

The function verifies the integrity of downloaded `.tar.gz` files using
`gzip -t` before using them. If a file is corrupted (which was causing
CI failures with "gzip: stdin: unexpected end of file" and "tar: Error
is not recoverable"), it is automatically removed and re-downloaded.

This fix is applied to:
- Kafka and Zookeeper downloads (`download_kafka`)
- Elasticsearch downloads (`download_elasticsearch`)
- OTEL Collector downloads (`download_otel_collector`)

This should resolve the flaky CI failures where corrupted partial downloads
were being cached and reused in subsequent test runs.

Co-authored-by: rgerhards <1482123+rgerhards@users.noreply.github.com>
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

2 issues found across 1 file (changes from recent commits).

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="tests/diag.sh">

<violation number="1" location="tests/diag.sh:2501">
P2: `((i++))` returns exit status 1 when `i` is 0 (post-increment evaluates to 0). This will cause premature script termination if `set -e` is ever enabled. Use `i=$((i + 1))` instead for safe arithmetic increment.</violation>

<violation number="2" location="tests/diag.sh:2502">
P2: `$TESTTOOL_DIR/msleep 2000` is used without checking if `TESTTOOL_DIR` is set or `msleep` exists. Other code in this diff defensively falls back to `sleep`. Add a similar guard here to avoid breaking the retry loop.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

printf '%s: wget failed, retrying...\n' "$name"
fi
rm -f "$dest"
((i++))
Copy link

@cubic-dev-ai cubic-dev-ai bot Feb 16, 2026

Choose a reason for hiding this comment

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

P2: ((i++)) returns exit status 1 when i is 0 (post-increment evaluates to 0). This will cause premature script termination if set -e is ever enabled. Use i=$((i + 1)) instead for safe arithmetic increment.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/diag.sh, line 2501:

<comment>`((i++))` returns exit status 1 when `i` is 0 (post-increment evaluates to 0). This will cause premature script termination if `set -e` is ever enabled. Use `i=$((i + 1))` instead for safe arithmetic increment.</comment>

<file context>
@@ -2463,51 +2463,78 @@ otel_exit_handling() {
+			printf '%s: wget failed, retrying...\n' "$name"
+		fi
+		rm -f "$dest"
+		((i++))
+		$TESTTOOL_DIR/msleep 2000
+	done
</file context>
Suggested change
((i++))
i=$((i + 1))
Fix with Cubic

fi
rm -f "$dest"
((i++))
$TESTTOOL_DIR/msleep 2000
Copy link

@cubic-dev-ai cubic-dev-ai bot Feb 16, 2026

Choose a reason for hiding this comment

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

P2: $TESTTOOL_DIR/msleep 2000 is used without checking if TESTTOOL_DIR is set or msleep exists. Other code in this diff defensively falls back to sleep. Add a similar guard here to avoid breaking the retry loop.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/diag.sh, line 2502:

<comment>`$TESTTOOL_DIR/msleep 2000` is used without checking if `TESTTOOL_DIR` is set or `msleep` exists. Other code in this diff defensively falls back to `sleep`. Add a similar guard here to avoid breaking the retry loop.</comment>

<file context>
@@ -2463,51 +2463,78 @@ otel_exit_handling() {
+		fi
+		rm -f "$dest"
+		((i++))
+		$TESTTOOL_DIR/msleep 2000
+	done
+
</file context>
Fix with Cubic

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