Skip to content

Conversation

bugBotter
Copy link
Owner

@bugBotter bugBotter commented Jul 3, 2025

Summary by CodeRabbit

  • New Features
    • Introduced a blacklist mechanism for OPcache, allowing users to exclude specific paths from caching using customizable patterns.
    • Supports loading blacklist entries from one or multiple files, with flexible pattern matching and platform compatibility.
    • Enhanced error handling and logging for blacklist file loading and pattern compilation.

Copy link

coderabbitai bot commented Jul 3, 2025

Walkthrough

A new source file introduces a blacklist mechanism for Zend OPcache, enabling exclusion of specific paths from caching via user-defined patterns. It manages entries, compiles them into PCRE2 regexes, loads patterns from files, normalizes paths, supports matching logic, and provides initialization, shutdown, and utility functions for external processing.

Changes

File(s) Change Summary
opcache/zend_accelerator_blacklist.c New file implementing a path blacklist system for OPcache, including pattern loading, regex compilation, matching, and resource management.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Blacklist
    participant FileSystem
    participant PCRE2

    User->>Blacklist: Initialize (zend_accel_blacklist_init)
    User->>Blacklist: Load patterns (zend_accel_blacklist_load)
    Blacklist->>FileSystem: Read blacklist file(s)
    Blacklist->>PCRE2: Compile patterns into regex
    User->>Blacklist: Check path (zend_accel_blacklist_is_blacklisted)
    Blacklist->>PCRE2: Match path against regex
    PCRE2-->>Blacklist: Return match result
    Blacklist-->>User: Return blacklist status
    User->>Blacklist: Shutdown (zend_accel_blacklist_shutdown)
    Blacklist->>PCRE2: Free regex resources
Loading

Poem

In the warren of code, a blacklist appears,
Guarding the cache from unwelcome peers.
Patterns and paths, with regex in tow,
Hopping through files, wherever they go.
Now OPcache is clever, with rules to obey—
A rabbit’s delight, for safe caching each day!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (4)
opcache/zend_accelerator_blacklist.c (4)

62-65: Remove unreachable code after fatal error.

The return statement after zend_accel_error_noreturn(ACCEL_LOG_FATAL, ...) is unreachable since fatal errors should not return. This could indicate a logical error or dead code.

  if (!blacklist->entries) {
    zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Blacklist initialization: no memory\n");
-   return;
  }

80-80: Consider dynamic buffer allocation for large patterns.

The fixed 12KB buffer size may be insufficient for very long blacklist patterns or numerous entries. Consider using dynamic allocation or increasing the buffer size.

- char regexp[12*1024], *p, *end, *c, *backtrack = NULL;
+ char *regexp = malloc(32*1024); // Or use dynamic sizing
+ if (!regexp) {
+   zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Failed to allocate regex buffer\n");
+   return;
+ }
+ char *p, *end, *c, *backtrack = NULL;

360-363: Consider logging allocation failures.

When match_data allocation fails, the function silently continues to the next pattern. This could mask serious memory issues and cause inconsistent blacklist behavior.

  if (!match_data) {
    /* Alloc failed, but next one could still come through and match. */
+   zend_accel_error(ACCEL_LOG_WARNING, "Failed to allocate match data for blacklist check\n");
    continue;
  }

1-384: Overall implementation assessment.

The blacklist implementation provides comprehensive functionality with good architectural design. However, there are several critical issues that need to be addressed:

  1. Memory safety: Missing NULL checks after allocation calls
  2. Buffer overflow protection: Fixed-size buffers may be insufficient
  3. Logic errors: Incorrect error handling in backtracking logic
  4. Thread safety: Consider thread safety implications of global variables

The core functionality is sound, but the implementation needs hardening for production use.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7626683 and 446d698.

📒 Files selected for processing (1)
  • opcache/zend_accelerator_blacklist.c (1 hunks)
🔇 Additional comments (2)
opcache/zend_accelerator_blacklist.c (2)

208-230: Well-implemented cleanup function.

The shutdown function properly handles NULL checks and frees all allocated resources in the correct order. Good defensive programming.


164-171: Fix logical error in backtracking logic.

When backtrack is NULL and the pattern is too long, the function logs an error but continues execution, which could lead to undefined behavior. The function should return early after logging the error.

  if (*c) {
    if (!backtrack) {
      zend_accel_error_noreturn(ACCEL_LOG_ERROR, "Too long blacklist entry\n");
+     return;
    }
    p = backtrack;
  } else {
    i++;
  }

Likely an incorrect or invalid review comment.

Comment on lines +234 to +237
if (blacklist->pos == blacklist->size) {
blacklist->size += ZEND_BLACKLIST_BLOCK_SIZE;
blacklist->entries = (zend_blacklist_entry *) realloc(blacklist->entries, sizeof(zend_blacklist_entry)*blacklist->size);
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add NULL check after realloc to prevent memory leak.

If realloc fails, it returns NULL but doesn't free the original memory. The current code would lose the original pointer, causing a memory leak and potential crash.

  if (blacklist->pos == blacklist->size) {
    blacklist->size += ZEND_BLACKLIST_BLOCK_SIZE;
-   blacklist->entries = (zend_blacklist_entry *) realloc(blacklist->entries, sizeof(zend_blacklist_entry)*blacklist->size);
+   zend_blacklist_entry *new_entries = (zend_blacklist_entry *) realloc(blacklist->entries, sizeof(zend_blacklist_entry)*blacklist->size);
+   if (!new_entries) {
+     zend_accel_error_noreturn(ACCEL_LOG_FATAL, "Failed to reallocate blacklist entries\n");
+     return;
+   }
+   blacklist->entries = new_entries;
  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (blacklist->pos == blacklist->size) {
blacklist->size += ZEND_BLACKLIST_BLOCK_SIZE;
blacklist->entries = (zend_blacklist_entry *) realloc(blacklist->entries, sizeof(zend_blacklist_entry)*blacklist->size);
}
if (blacklist->pos == blacklist->size) {
blacklist->size += ZEND_BLACKLIST_BLOCK_SIZE;
zend_blacklist_entry *new_entries = (zend_blacklist_entry *) realloc(
blacklist->entries,
sizeof(zend_blacklist_entry) * blacklist->size
);
if (!new_entries) {
zend_accel_error_noreturn(
ACCEL_LOG_FATAL,
"Failed to reallocate blacklist entries\n"
);
return;
}
blacklist->entries = new_entries;
}
🤖 Prompt for AI Agents
In opcache/zend_accelerator_blacklist.c around lines 234 to 237, the code calls
realloc without checking if it returns NULL, risking a memory leak if allocation
fails. Modify the code to store the result of realloc in a temporary pointer,
check if it is NULL, and only assign it back to blacklist->entries if
successful. If realloc returns NULL, handle the error appropriately without
losing the original pointer.

Comment on lines +278 to +282
/* strip \" */
if (pbuf[0] == '\"' && pbuf[path_length - 1]== '\"') {
*pbuf++ = 0;
path_length -= 2;
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve quote stripping validation.

The quote stripping logic assumes quotes are at both start and end but doesn't validate path_length >= 2 before accessing pbuf[path_length - 1].

  /* strip \" */
- if (pbuf[0] == '\"' && pbuf[path_length - 1]== '\"') {
+ if (path_length >= 2 && pbuf[0] == '\"' && pbuf[path_length - 1] == '\"') {
    *pbuf++ = 0;
    path_length -= 2;
  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/* strip \" */
if (pbuf[0] == '\"' && pbuf[path_length - 1]== '\"') {
*pbuf++ = 0;
path_length -= 2;
}
/* strip \" */
if (path_length >= 2 && pbuf[0] == '\"' && pbuf[path_length - 1] == '\"') {
*pbuf++ = 0;
path_length -= 2;
}
🤖 Prompt for AI Agents
In opcache/zend_accelerator_blacklist.c around lines 278 to 282, the code strips
quotes from a string but does not check if path_length is at least 2 before
accessing pbuf[path_length - 1], which can cause out-of-bounds access. Fix this
by adding a condition to ensure path_length >= 2 before checking and stripping
the quotes at both ends.

Comment on lines +188 to +194
if (PCRE_G(jit)) {
if (0 > pcre2_jit_compile(it->re, PCRE2_JIT_COMPLETE)) {
/* Don't return here, even JIT could fail to compile, the pattern is still usable. */
pcre2_get_error_message(errnumber, pcre_error, sizeof(pcre_error));
zend_accel_error(ACCEL_LOG_WARNING, "Blacklist JIT compilation failed, %s\n", pcre_error);
}
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix incorrect error number usage in JIT compilation error.

The errnumber variable contains the error from pcre2_compile, not from pcre2_jit_compile. The JIT compilation error should use the return value from pcre2_jit_compile.

  if (PCRE_G(jit)) {
-   if (0 > pcre2_jit_compile(it->re, PCRE2_JIT_COMPLETE)) {
+   int jit_error = pcre2_jit_compile(it->re, PCRE2_JIT_COMPLETE);
+   if (jit_error < 0) {
      /* Don't return here, even JIT could fail to compile, the pattern is still usable. */
-     pcre2_get_error_message(errnumber, pcre_error, sizeof(pcre_error));
+     pcre2_get_error_message(jit_error, pcre_error, sizeof(pcre_error));
      zend_accel_error(ACCEL_LOG_WARNING, "Blacklist JIT compilation failed, %s\n", pcre_error);
    }
  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (PCRE_G(jit)) {
if (0 > pcre2_jit_compile(it->re, PCRE2_JIT_COMPLETE)) {
/* Don't return here, even JIT could fail to compile, the pattern is still usable. */
pcre2_get_error_message(errnumber, pcre_error, sizeof(pcre_error));
zend_accel_error(ACCEL_LOG_WARNING, "Blacklist JIT compilation failed, %s\n", pcre_error);
}
}
if (PCRE_G(jit)) {
int jit_error = pcre2_jit_compile(it->re, PCRE2_JIT_COMPLETE);
if (jit_error < 0) {
/* Don't return here, even JIT could fail to compile, the pattern is still usable. */
pcre2_get_error_message(jit_error, pcre_error, sizeof(pcre_error));
zend_accel_error(ACCEL_LOG_WARNING, "Blacklist JIT compilation failed, %s\n", pcre_error);
}
}
🤖 Prompt for AI Agents
In opcache/zend_accelerator_blacklist.c around lines 188 to 194, the error
number used for the JIT compilation failure message is incorrectly taken from
the pcre2_compile error instead of the pcre2_jit_compile return value. Modify
the code to capture the return value of pcre2_jit_compile into a variable and
use that variable as the error number when calling pcre2_get_error_message to
ensure the correct error details are logged.

Comment on lines +271 to +276
/* Strip ctrl-m prefix */
pbuf = &buf[0];
while (*pbuf == '\r') {
*pbuf++ = 0;
path_length--;
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix ctrl-m stripping logic.

The current logic modifies pbuf but decrements path_length, which could lead to incorrect length calculations. The loop should either modify the buffer in-place or adjust the pointer correctly.

  /* Strip ctrl-m prefix */
  pbuf = &buf[0];
  while (*pbuf == '\r') {
-   *pbuf++ = 0;
+   pbuf++;
    path_length--;
  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/* Strip ctrl-m prefix */
pbuf = &buf[0];
while (*pbuf == '\r') {
*pbuf++ = 0;
path_length--;
}
/* Strip ctrl-m prefix */
pbuf = &buf[0];
while (*pbuf == '\r') {
pbuf++;
path_length--;
}
🤖 Prompt for AI Agents
In opcache/zend_accelerator_blacklist.c around lines 271 to 276, the ctrl-m
stripping loop incorrectly decrements path_length while advancing pbuf, causing
potential length miscalculations. Fix this by either shifting the buffer
contents left to remove all leading '\r' characters and adjusting path_length
accordingly, or by moving the pointer without modifying the buffer and
recalculating path_length based on the new start position. Ensure path_length
accurately reflects the length of the resulting string after stripping.

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