-
-
Notifications
You must be signed in to change notification settings - Fork 274
修复xy.h中所有可能导致潜在内存泄漏的代码 #344
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ | |
| * | BingChunMoLi <[email protected]> | ||
| * | | ||
| * Created On : <2023-08-28> | ||
| * Last Modified : <2025-12-31> | ||
| * Last Modified : <2026-03-17> | ||
| * | ||
| * | ||
| * xy: 襄阳、咸阳 | ||
|
|
@@ -23,7 +23,7 @@ | |
| #ifndef XY_H | ||
| #define XY_H | ||
|
|
||
| #define _XY_Version "v0.2.2.0-2025/10/28" | ||
| #define _XY_Version "v0.2.2.1-2026/03/17" | ||
| #define _XY_Maintain_URL "https://github.com/RubyMetric/chsrc/blob/dev/lib/xy.h" | ||
| #define _XY_Maintain_URL2 "https://gitee.com/RubyMetric/chsrc/blob/dev/lib/xy.h" | ||
|
|
||
|
|
@@ -261,14 +261,12 @@ xy_str_gsub (const char *str, const char *pat, const char *replace) | |
| size_t replace_len = strlen (replace); | ||
| size_t pat_len = strlen (pat); | ||
|
|
||
| int unit = replace_len - pat_len; | ||
| if (unit <= 0) | ||
| unit = 0; | ||
| size_t unit = (replace_len > pat_len) ? (replace_len - pat_len) : 0; | ||
|
|
||
| size_t len = strlen (str); | ||
|
|
||
| const char *cur = str; | ||
| int count = 0; | ||
| size_t count = 0; | ||
|
|
||
| while (cur < str + len) | ||
| { | ||
|
|
@@ -284,6 +282,7 @@ xy_str_gsub (const char *str, const char *pat, const char *replace) | |
| // puti(count); DEBUG 匹配次数 | ||
|
|
||
| char *ret = malloc (unit * count + len + 1); | ||
| if (!ret) return NULL; | ||
| char *retcur = ret; | ||
|
|
||
| cur = str; | ||
|
|
@@ -379,9 +378,9 @@ xy_strcat (unsigned int count, ...) | |
| cur = ret + diff; | ||
| } | ||
| if (NULL == ret) | ||
| { | ||
| { | ||
| _xy_internal_warn ("xy_strcat(): No availble memory to allocate!"); | ||
| return NULL; | ||
| return NULL; | ||
| } | ||
| strcpy (cur, str); | ||
| // puts(ret); | ||
|
|
@@ -485,10 +484,9 @@ _xy_str_to_terminal_style (int style, const char *str) | |
| } | ||
|
|
||
|
|
||
| size_t len = 0; | ||
| new_str: | ||
| // -2 把中间%s减掉 | ||
| len = strlen (color_fmt_str) - 2; | ||
| size_t len = strlen (color_fmt_str) - 2; | ||
| char *buf = malloc (strlen (str) + len + 1); | ||
| sprintf (buf, color_fmt_str, str); | ||
| return buf; | ||
|
|
@@ -969,6 +967,7 @@ xy_run_iter_lines (const char *cmd, unsigned long n, bool (*func) (const char | |
| if (stream == NULL) | ||
| { | ||
| _xy_internal_warn ("xy_run_iter_lines(): popen() failed"); | ||
| free (buf); | ||
| return NULL; | ||
| } | ||
|
|
||
|
|
@@ -980,6 +979,8 @@ xy_run_iter_lines (const char *cmd, unsigned long n, bool (*func) (const char | |
| if (NULL == fgets (buf, size, stream)) | ||
| break; | ||
| /* 存在换行的总是会把换行符读出来,删掉 */ | ||
| if (ret) | ||
| free (ret); | ||
| ret = xy_str_delete_suffix (buf, "\n"); | ||
| count += 1; | ||
| if (n == count) | ||
|
|
@@ -991,6 +992,7 @@ xy_run_iter_lines (const char *cmd, unsigned long n, bool (*func) (const char | |
| } | ||
| } | ||
|
|
||
| free (buf); | ||
| pclose (stream); | ||
| return ret; | ||
| } | ||
|
|
@@ -1018,6 +1020,7 @@ xy_run_get_status (char *cmd) | |
| char * command = xy_quiet_cmd (cmd); | ||
|
|
||
| int status = system (command); | ||
| free (command); | ||
| return status; | ||
| } | ||
|
|
||
|
|
@@ -1041,6 +1044,7 @@ xy_run_get_stdout (const char *cmd, char **output) | |
| if (stream == NULL) | ||
| { | ||
| _xy_internal_warn ("xy_run_get_stdout(): popen() failed"); | ||
| free (buf); | ||
| return -1; | ||
| } | ||
|
|
||
|
|
@@ -1061,6 +1065,8 @@ xy_run_get_stdout (const char *cmd, char **output) | |
|
|
||
| if (output) | ||
| *output = buf; | ||
| else | ||
| free (buf); | ||
|
|
||
| return status; | ||
| } | ||
|
|
@@ -1299,7 +1305,8 @@ xy_parent_dir (const char *path) | |
| if (!last) | ||
| { | ||
| /* 路径中没有一个 / 是很奇怪的,我们直接返回 . 作为当前目录 */ | ||
| return "."; | ||
| free (dir); | ||
| return xy_strdup ("."); | ||
Mikachu2333 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| *last = '\0'; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.