Skip to content

Commit 11508f4

Browse files
committed
修复除和之外的内存泄露
1 parent a56f330 commit 11508f4

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

lib/xy.h

Lines changed: 18 additions & 11 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;
@@ -379,9 +378,9 @@ xy_strcat (unsigned int count, ...)
379378
cur = ret + diff;
380379
}
381380
if (NULL == ret)
382-
{
381+
{
383382
_xy_internal_warn ("xy_strcat(): No availble memory to allocate!");
384-
return NULL;
383+
return NULL;
385384
}
386385
strcpy (cur, str);
387386
// puts(ret);
@@ -485,10 +484,9 @@ _xy_str_to_terminal_style (int style, const char *str)
485484
}
486485

487486

488-
size_t len = 0;
489487
new_str:
490488
// -2 把中间%s减掉
491-
len = strlen (color_fmt_str) - 2;
489+
size_t len = strlen (color_fmt_str) - 2;
492490
char *buf = malloc (strlen (str) + len + 1);
493491
sprintf (buf, color_fmt_str, str);
494492
return buf;
@@ -969,6 +967,7 @@ xy_run_iter_lines (const char *cmd, unsigned long n, bool (*func) (const char
969967
if (stream == NULL)
970968
{
971969
_xy_internal_warn ("xy_run_iter_lines(): popen() failed");
970+
free (buf);
972971
return NULL;
973972
}
974973

@@ -980,6 +979,8 @@ xy_run_iter_lines (const char *cmd, unsigned long n, bool (*func) (const char
980979
if (NULL == fgets (buf, size, stream))
981980
break;
982981
/* 存在换行的总是会把换行符读出来,删掉 */
982+
if (ret)
983+
free (ret);
983984
ret = xy_str_delete_suffix (buf, "\n");
984985
count += 1;
985986
if (n == count)
@@ -991,6 +992,7 @@ xy_run_iter_lines (const char *cmd, unsigned long n, bool (*func) (const char
991992
}
992993
}
993994

995+
free (buf);
994996
pclose (stream);
995997
return ret;
996998
}
@@ -1018,6 +1020,7 @@ xy_run_get_status (char *cmd)
10181020
char * command = xy_quiet_cmd (cmd);
10191021

10201022
int status = system (command);
1023+
free (command);
10211024
return status;
10221025
}
10231026

@@ -1041,6 +1044,7 @@ xy_run_get_stdout (const char *cmd, char **output)
10411044
if (stream == NULL)
10421045
{
10431046
_xy_internal_warn ("xy_run_get_stdout(): popen() failed");
1047+
free (buf);
10441048
return -1;
10451049
}
10461050

@@ -1061,6 +1065,8 @@ xy_run_get_stdout (const char *cmd, char **output)
10611065

10621066
if (output)
10631067
*output = buf;
1068+
else
1069+
free (buf);
10641070

10651071
return status;
10661072
}
@@ -1299,7 +1305,8 @@ xy_parent_dir (const char *path)
12991305
if (!last)
13001306
{
13011307
/* 路径中没有一个 / 是很奇怪的,我们直接返回 . 作为当前目录 */
1302-
return ".";
1308+
free (dir);
1309+
return xy_strdup (".");
13031310
}
13041311
*last = '\0';
13051312

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)