-
Notifications
You must be signed in to change notification settings - Fork 4.4k
ncnn2mem support new style array and string type #6440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
77167c3
8aae03a
53e2d79
c4d4591
b64ff83
d5777d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -65,6 +65,11 @@ static bool vstr_is_float(const char vstr[16]) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static bool vstr_is_string(const char vstr[16]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return isalpha(vstr[0]) || vstr[0] == '\"'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static float vstr_to_float(const char vstr[16]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| double v = 0.0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -281,12 +286,13 @@ static int dump_param(const char* parampath, const char* parambinpath, const cha | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int id = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while (fscanf(fp, "%d=", &id) == 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fwrite(&id, sizeof(int), 1, mp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool is_array = id <= -23300; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (is_array) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fwrite(&id, sizeof(int), 1, mp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // old style array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
nihui marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int len = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nscan = fscanf(fp, "%d", &len); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (nscan != 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -320,18 +326,157 @@ static int dump_param(const char* parampath, const char* parambinpath, const cha | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fwrite(&v, sizeof(int), 1, mp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| char vstr[16]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| char comma[4]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nscan = fscanf(fp, "%15[^,\n ]", vstr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (nscan != 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| char vstr[16]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nscan = fscanf(fp, "%15s", vstr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (nscan != 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fprintf(stderr, "read value failed %d\n", nscan); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return -1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool is_string = vstr_is_string(vstr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (is_string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | |
| { | |
| // 23400 is the encoding offset for string types (similar to 23300 for arrays) |
nihui marked this conversation as resolved.
Show resolved
Hide resolved
nihui marked this conversation as resolved.
Show resolved
Hide resolved
nihui marked this conversation as resolved.
Show resolved
Hide resolved
Copilot
AI
Dec 5, 2025
There was a problem hiding this comment.
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.
nihui marked this conversation as resolved.
Show resolved
Hide resolved
nihui marked this conversation as resolved.
Show resolved
Hide resolved
Copilot
AI
Dec 5, 2025
There was a problem hiding this comment.
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);
}| 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
AI
Dec 5, 2025
There was a problem hiding this comment.
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.
| 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); |
nihui marked this conversation as resolved.
Show resolved
Hide resolved
Copilot
AI
Dec 5, 2025
There was a problem hiding this comment.
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);| if (str[str.size() - 1] == '\"') | |
| if (!str.empty() && str[str.size() - 1] == '\"') |
Uh oh!
There was an error while loading. Please reload this page.