Skip to content

Commit eb10392

Browse files
committed
修复xy.h中所有可能导致潜在内存泄漏的代码
1 parent a56f330 commit eb10392

File tree

2 files changed

+75
-27
lines changed

2 files changed

+75
-27
lines changed

lib/xy.h

Lines changed: 74 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* | BingChunMoLi <bingchunmoli@bingchunmoli.com>
1010
* |
1111
* Created On : <2023-08-28>
12-
* Last Modified : <2025-12-31>
12+
* Last Modified : <2026-03-17>
1313
*
1414
*
1515
* xy: 襄阳、咸阳
@@ -23,7 +23,7 @@
2323
#ifndef XY_H
2424
#define XY_H
2525

26-
#define _XY_Version "v0.2.2.0-2025/10/28"
26+
#define _XY_Version "v0.2.2.1-2026/03/17"
2727
#define _XY_Maintain_URL "https://github.com/RubyMetric/chsrc/blob/dev/lib/xy.h"
2828
#define _XY_Maintain_URL2 "https://gitee.com/RubyMetric/chsrc/blob/dev/lib/xy.h"
2929

@@ -261,14 +261,12 @@ xy_str_gsub (const char *str, const char *pat, const char *replace)
261261
size_t replace_len = strlen (replace);
262262
size_t pat_len = strlen (pat);
263263

264-
int unit = replace_len - pat_len;
265-
if (unit <= 0)
266-
unit = 0;
264+
size_t unit = (replace_len > pat_len) ? (replace_len - pat_len) : 0;
267265

268266
size_t len = strlen (str);
269267

270268
const char *cur = str;
271-
int count = 0;
269+
size_t count = 0;
272270

273271
while (cur < str + len)
274272
{
@@ -284,6 +282,7 @@ xy_str_gsub (const char *str, const char *pat, const char *replace)
284282
// puti(count); DEBUG 匹配次数
285283

286284
char *ret = malloc (unit * count + len + 1);
285+
if (!ret) return NULL;
287286
char *retcur = ret;
288287

289288
cur = str;
@@ -375,14 +374,17 @@ xy_strcat (unsigned int count, ...)
375374
if (need_realloc)
376375
{
377376
ptrdiff_t diff = cur - ret;
378-
ret = realloc (ret, al_cur);
377+
char *new_ret = realloc (ret, al_cur);
378+
if (NULL == new_ret)
379+
{
380+
_xy_internal_warn ("xy_strcat(): No available memory to allocate!");
381+
free (ret);
382+
va_end (args);
383+
return NULL;
384+
}
385+
ret = new_ret;
379386
cur = ret + diff;
380387
}
381-
if (NULL == ret)
382-
{
383-
_xy_internal_warn ("xy_strcat(): No availble memory to allocate!");
384-
return NULL;
385-
}
386388
strcpy (cur, str);
387389
// puts(ret);
388390
cur += strlen (str);
@@ -485,11 +487,15 @@ _xy_str_to_terminal_style (int style, const char *str)
485487
}
486488

487489

488-
size_t len = 0;
489490
new_str:
490491
// -2 把中间%s减掉
491-
len = strlen (color_fmt_str) - 2;
492+
size_t len = strlen (color_fmt_str) - 2;
492493
char *buf = malloc (strlen (str) + len + 1);
494+
if (!buf)
495+
{
496+
_xy_internal_warn ("_xy_str_to_terminal_style(): No available memory to allocate!");
497+
return NULL;
498+
}
493499
sprintf (buf, color_fmt_str, str);
494500
return buf;
495501
}
@@ -804,20 +810,28 @@ _xy_log (int level, const char *prompt, const char *content)
804810
}
805811
else if (level & _XY_Log_Success)
806812
{
807-
str = xy_strcat (3, prompt, ": ", xy_str_to_green (content));
813+
char *colored = xy_str_to_green (content);
814+
str = xy_strcat (3, prompt, ": ", colored);
815+
free (colored);
808816
}
809817
else if (level & _XY_Log_Info)
810818
{
811-
str = xy_strcat (3, prompt, ": ", xy_str_to_blue (content));
819+
char *colored = xy_str_to_blue (content);
820+
str = xy_strcat (3, prompt, ": ", colored);
821+
free (colored);
812822
}
813823
else if (level & _XY_Log_Warn)
814824
{
815-
str = xy_strcat (3, prompt, ": ", xy_str_to_yellow (content));
825+
char *colored = xy_str_to_yellow (content);
826+
str = xy_strcat (3, prompt, ": ", colored);
827+
free (colored);
816828
to_stderr = true;
817829
}
818830
else if (level & _XY_Log_Error)
819831
{
820-
str = xy_strcat (3, prompt, ": ", xy_str_to_red (content));
832+
char *colored = xy_str_to_red (content);
833+
str = xy_strcat (3, prompt, ": ", colored);
834+
free (colored);
821835
to_stderr = true;
822836
}
823837
else
@@ -881,29 +895,45 @@ _xy_log_brkt (int level, const char *prompt1, const char *prompt2, const char *c
881895
else if (level & _XY_Log_Success)
882896
{
883897
/* [app 成功] [app success] */
884-
str = xy_strcat (6,
885-
"[", xy_str_to_green (prompt1), " ", xy_str_to_bold (xy_str_to_green (prompt2)), "] ", xy_str_to_green (content));
898+
char *p1 = xy_str_to_green (prompt1);
899+
char *p2_inner = xy_str_to_green (prompt2);
900+
char *p2 = xy_str_to_bold (p2_inner);
901+
char *c = xy_str_to_green (content);
902+
str = xy_strcat (6, "[", p1, " ", p2, "] ", c);
903+
free (p1); free (p2_inner); free (p2); free (c);
886904
}
887905
else if (level & _XY_Log_Info)
888906
{
889907
/* [app 信息] [app info]
890908
[app 提示] [app notice]
891909
*/
892-
str = xy_strcat (6,
893-
"[", xy_str_to_blue (prompt1), " ", xy_str_to_bold (xy_str_to_blue (prompt2)), "] ", xy_str_to_blue (content));
910+
char *p1 = xy_str_to_blue (prompt1);
911+
char *p2_inner = xy_str_to_blue (prompt2);
912+
char *p2 = xy_str_to_bold (p2_inner);
913+
char *c = xy_str_to_blue (content);
914+
str = xy_strcat (6, "[", p1, " ", p2, "] ", c);
915+
free (p1); free (p2_inner); free (p2); free (c);
894916
}
895917
else if (level & _XY_Log_Warn)
896918
{
897919
/* [app 警告] [app warn] */
898-
str = xy_strcat (6,
899-
"[", xy_str_to_yellow (prompt1), " ", xy_str_to_bold (xy_str_to_yellow (prompt2)), "] ", xy_str_to_yellow (content));
920+
char *p1 = xy_str_to_yellow (prompt1);
921+
char *p2_inner = xy_str_to_yellow (prompt2);
922+
char *p2 = xy_str_to_bold (p2_inner);
923+
char *c = xy_str_to_yellow (content);
924+
str = xy_strcat (6, "[", p1, " ", p2, "] ", c);
925+
free (p1); free (p2_inner); free (p2); free (c);
900926
to_stderr = true;
901927
}
902928
else if (level & _XY_Log_Error)
903929
{
904930
/* [app 错误] [app error] */
905-
str = xy_strcat (6,
906-
"[", xy_str_to_red (prompt1), " ", xy_str_to_bold (xy_str_to_red (prompt2)), "] ", xy_str_to_red (content));
931+
char *p1 = xy_str_to_red (prompt1);
932+
char *p2_inner = xy_str_to_red (prompt2);
933+
char *p2 = xy_str_to_bold (p2_inner);
934+
char *c = xy_str_to_red (content);
935+
str = xy_strcat (6, "[", p1, " ", p2, "] ", c);
936+
free (p1); free (p2_inner); free (p2); free (c);
907937
to_stderr = true;
908938
}
909939
else
@@ -969,6 +999,7 @@ xy_run_iter_lines (const char *cmd, unsigned long n, bool (*func) (const char
969999
if (stream == NULL)
9701000
{
9711001
_xy_internal_warn ("xy_run_iter_lines(): popen() failed");
1002+
free (buf);
9721003
return NULL;
9731004
}
9741005

@@ -980,6 +1011,8 @@ xy_run_iter_lines (const char *cmd, unsigned long n, bool (*func) (const char
9801011
if (NULL == fgets (buf, size, stream))
9811012
break;
9821013
/* 存在换行的总是会把换行符读出来,删掉 */
1014+
if (ret)
1015+
free (ret);
9831016
ret = xy_str_delete_suffix (buf, "\n");
9841017
count += 1;
9851018
if (n == count)
@@ -991,6 +1024,7 @@ xy_run_iter_lines (const char *cmd, unsigned long n, bool (*func) (const char
9911024
}
9921025
}
9931026

1027+
free (buf);
9941028
pclose (stream);
9951029
return ret;
9961030
}
@@ -1018,6 +1052,7 @@ xy_run_get_status (char *cmd)
10181052
char * command = xy_quiet_cmd (cmd);
10191053

10201054
int status = system (command);
1055+
free (command);
10211056
return status;
10221057
}
10231058

@@ -1041,6 +1076,7 @@ xy_run_get_stdout (const char *cmd, char **output)
10411076
if (stream == NULL)
10421077
{
10431078
_xy_internal_warn ("xy_run_get_stdout(): popen() failed");
1079+
free (buf);
10441080
return -1;
10451081
}
10461082

@@ -1052,6 +1088,14 @@ xy_run_get_stdout (const char *cmd, char **output)
10521088
{
10531089
cap *= 2;
10541090
char *new_buf = realloc (buf, cap);
1091+
if (NULL == new_buf)
1092+
{
1093+
_xy_internal_warn ("xy_run_get_stdout(): No available memory to allocate!");
1094+
free (buf);
1095+
pclose (stream);
1096+
if (output) *output = NULL;
1097+
return -1;
1098+
}
10551099
buf = new_buf;
10561100
}
10571101
}
@@ -1061,6 +1105,8 @@ xy_run_get_stdout (const char *cmd, char **output)
10611105

10621106
if (output)
10631107
*output = buf;
1108+
else
1109+
free (buf);
10641110

10651111
return status;
10661112
}
@@ -1299,7 +1345,8 @@ xy_parent_dir (const char *path)
12991345
if (!last)
13001346
{
13011347
/* 路径中没有一个 / 是很奇怪的,我们直接返回 . 作为当前目录 */
1302-
return ".";
1348+
free (dir);
1349+
return xy_strdup (".");
13031350
}
13041351
*last = '\0';
13051352

test/xy.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ main (int argc, char const *argv[])
145145

146146
println (xy_normalize_path (" \n ~/haha/test/123 \n\r "));
147147
assert_str (xy_normalize_path ("~/haha/test"), xy_parent_dir (" ~/haha/test/123"));
148+
assert_str (".", xy_parent_dir ("abc"));
148149

149150

150151

0 commit comments

Comments
 (0)