Skip to content

Conversation

@nihui
Copy link
Member

@nihui nihui commented Dec 5, 2025

No description provided.

@nihui nihui requested a review from Copilot December 5, 2025 07:14
@github-actions github-actions bot added the tool label Dec 5, 2025
@tencent-adm
Copy link
Member

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot finished reviewing on behalf of nihui December 5, 2025 07:17
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request enhances the ncnn2mem tool to support new style arrays (comma-separated values like 1,2,3) and string type parameters in addition to the existing old style arrays (length-prefixed like -23300=3,1,2,3).

Key changes:

  • Added vstr_is_string() function to detect string parameters
  • Refactored parameter parsing to handle three types: old style arrays (id <= -23300), strings (-id - 23400), and new style comma-separated arrays (-id - 23300)
  • Implemented string parsing with support for quoted and unquoted strings

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +357 to +363
char end = vstr[len - 1];
if (end != '\"')
{
nscan = fscanf(fp, "%255[^\"\n]\"", vstr2);
}
else
nscan = 0; // already ended with a quote, no need to scan more
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

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

The len calculation can cause undefined behavior when vstr is empty or only contains a null terminator. If vstr[0] == '\"' and the string has length 1 (only a quote), then len would be 1 and accessing vstr[len - 1] (i.e., vstr[0]) would incorrectly check the opening quote instead of detecting there's no closing quote.

Consider checking if len > 1 before accessing vstr[len - 1] to avoid edge cases:

int len = 0;
while (vstr[len] != '\0')
    len++;
if (len > 1)
{
    char end = vstr[len - 1];
    if (end != '\"')
    {
        nscan = fscanf(fp, "%255[^\"\n]\"", vstr2);
    }
    else
        nscan = 0;
}
else if (len == 1)
{
    // Only opening quote, need to read the rest
    nscan = fscanf(fp, "%255[^\"\n]\"", vstr2);
}
Suggested change
char end = vstr[len - 1];
if (end != '\"')
{
nscan = fscanf(fp, "%255[^\"\n]\"", vstr2);
}
else
nscan = 0; // already ended with a quote, no need to scan more
if (len > 1)
{
char end = vstr[len - 1];
if (end != '\"')
{
nscan = fscanf(fp, "%255[^\"\n]\"", vstr2);
}
else
nscan = 0; // already ended with a quote, no need to scan more
}
else if (len == 1)
{
// Only opening quote, need to read the rest
nscan = fscanf(fp, "%255[^\"\n]\"", vstr2);
}
else
{
// Empty string, nothing to scan
nscan = 0;
}

Copilot uses AI. Check for mistakes.
Comment on lines +360 to +367
nscan = fscanf(fp, "%255[^\"\n]\"", vstr2);
}
else
nscan = 0; // already ended with a quote, no need to scan more
}
else
{
nscan = fscanf(fp, "%255[^\n ]", vstr2);
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

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

The format string "%255[^\n ]" can read up to 255 characters into vstr2 (size 256), which is correct. However, the subsequent validation at line 373 checks vstr2[241] instead of checking if the buffer was fully utilized. This inconsistency means strings between 241 and 255 characters would pass validation even though they exceed the intended limit (15 from vstr + 240 from vstr2 = 255 max).

Either adjust the format to "%240[^\n ]" to enforce the 255 total limit, or fix the validation to check the appropriate position.

Suggested change
nscan = fscanf(fp, "%255[^\"\n]\"", vstr2);
}
else
nscan = 0; // already ended with a quote, no need to scan more
}
else
{
nscan = fscanf(fp, "%255[^\n ]", vstr2);
nscan = fscanf(fp, "%240[^\"\n]\"", vstr2);
}
else
nscan = 0; // already ended with a quote, no need to scan more
}
else
{
nscan = fscanf(fp, "%240[^\n ]", vstr2);

Copilot uses AI. Check for mistakes.
str = std::string(vstr);
}

if (str[str.size() - 1] == '\"')
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

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

Accessing str[str.size() - 1] without first checking if str is empty can cause undefined behavior. If the string is empty (which could happen if vstr[0] == '\"' and vstr[1] == '\0'), then str.size() would be 0, and str.size() - 1 would wrap around to a very large number due to unsigned arithmetic.

Add a check before accessing the last character:

if (!str.empty() && str[str.size() - 1] == '\"')
    str.resize(str.size() - 1);
Suggested change
if (str[str.size() - 1] == '\"')
if (!str.empty() && str[str.size() - 1] == '\"')

Copilot uses AI. Check for mistakes.

// scan the remaining string
char vstr2[256];
vstr2[241] = '\0'; // max 255 = 15 + 240
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

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

The magic number 241 and the comment explaining "max 255 = 15 + 240" suggest a constraint, but using 241 as a sentinel is confusing. Consider using a named constant to clarify the intent:

const int MAX_VSTR2_LEN = 240;
char vstr2[MAX_VSTR2_LEN + 1];
vstr2[MAX_VSTR2_LEN] = '\0';

Then check vstr2[MAX_VSTR2_LEN] != '\0' at line 373. This makes the relationship between the sizes explicit.

Copilot uses AI. Check for mistakes.

bool is_string = vstr_is_string(vstr);
if (is_string)
{
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

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

The transformation id = -id - 23400 is a magic number calculation. Consider adding a comment explaining that 23400 is the encoding offset for string types, similar to how 23300 is used for arrays. This would improve code readability and maintainability.

Suggested change
{
{
// 23400 is the encoding offset for string types (similar to 23300 for arrays)

Copilot uses AI. Check for mistakes.
@github-actions
Copy link

github-actions bot commented Dec 5, 2025

The binary size change of libncnn.so (bytes)

architecture base size pr size difference
x86_64 15316400 15316400 0 😘
armhf 6229892 6229892 0 😘
aarch64 9527616 9527616 0 😘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants