From 35c6dbce44e8ccfde2db2c11121596d7b62873f7 Mon Sep 17 00:00:00 2001 From: lilyco Date: Fri, 27 Nov 2026 22:06:50 +0800 Subject: [PATCH 1/3] fix: guard empty/malformed curl output in speed measurement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit measure_speed_for_url() returns NULL when curl produces no output (e.g. TLS handshake failure), and parse_and_say_curl_result() dereferenced it via strchr(), causing a segmentation fault during [chsrc 测速] 进行中  ^ 上游默认源 (https://crates.io/) [精准测速] ... 303.48 KByte/s  - 校园网联合镜像站(MirrorZ) [模糊测速] ... 10.85 MByte/s  - 字节跳动基础架构Dev Infra [精准测速] ... 1.86 MByte/s  - 阿里巴巴开源镜像站(公网) [精准测速] ... 10.50 MByte/s  - 浙江大学开源软件镜像站 [模糊测速] ... 97.96 KByte/s  - 上海交通大学致远镜像站 [精准测速] ... 8.90 MByte/s  - 清华大学开源软件镜像站 [模糊测速] ... 10.79 MByte/s  - 北京外国语大学开源软件镜像站 [模糊测速] ... 10.93 MByte/s  - 中国科学技术大学开源软件镜像 [精准测速] ... 错误 curl 输出: 000 最快镜像站: 北京外国语大学开源软件镜像站 选中镜像站: BFSU (bfsu) -------------------------------- [chsrc 备份] /home/lilyco/.cargo/config.toml -> /home/lilyco/.cargo/config.toml.bak [chsrc 覆写] /home/lilyco/.cargo/config.toml -------------------------------- 全自动换源完成, 感谢镜像提供方: 北京外国语大学开源软件镜像站 食谱更新: 2026-09-06 后厨人数: 4 (详查 chsrc ls rust) when measuring the USTC crates.io proxy. Also handle output without a space (missing speed field) gracefully instead of dereferencing a NULL split pointer. --- src/framework/source.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/framework/source.c b/src/framework/source.c index 50bb6b0d6..55c2cf529 100644 --- a/src/framework/source.c +++ b/src/framework/source.c @@ -112,9 +112,25 @@ measure_speed_for_url (void *url) static double parse_and_say_curl_result (char *curl_buf) { + /* curl 失败时可能无任何输出,此时须软处理而非崩溃 */ + if (NULL==curl_buf || '\0'==curl_buf[0]) + { + char *msg = ENGLISH ? "ERROR curl output is empty (connection failed?): " : "错误 curl 无输出 (连接失败?): "; + println (red (msg)); + return -1; + } + /* 分隔两部分数据 */ char *split = strchr (curl_buf, ' '); - if (split) *split = '\0'; + if (NULL==split) + { + /* 输出不含空格,格式异常,软处理 */ + char *msg = ENGLISH ? "ERROR malformed curl output: " : "错误 curl 输出格式异常: "; + println (red (xy_2strcat (msg, curl_buf))); + return -1; + } + + *split = '\0'; // say(curl_buf); say(split+1); int http_code = xy_str2int (curl_buf); From db9cff0979e2117f783f7a57f0208c014fae6213 Mon Sep 17 00:00:00 2001 From: lilyco-42 <251102081+lilyco-42@users.noreply.github.com> Date: Sun, 29 Nov 2026 11:21:27 +0800 Subject: [PATCH 2/3] fix: add bug-report hint to curl output guard --- src/framework/source.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/framework/source.c b/src/framework/source.c index 55c2cf529..1068d165d 100644 --- a/src/framework/source.c +++ b/src/framework/source.c @@ -112,10 +112,13 @@ measure_speed_for_url (void *url) static double parse_and_say_curl_result (char *curl_buf) { - /* curl 失败时可能无任何输出,此时须软处理而非崩溃 */ + /* curl 失败时可能无任何输出,此时须软处理而非崩溃。 + * 理论上走不到这里:一旦触发,说明我们正在排查的 Bug 真实发生,请让用户报告以便定位。 */ if (NULL==curl_buf || '\0'==curl_buf[0]) { - char *msg = ENGLISH ? "ERROR curl output is empty (connection failed?): " : "错误 curl 无输出 (连接失败?): "; + char *msg = ENGLISH + ? "ERROR curl output is empty (connection failed?). This is a bug under investigation, please report: chsrc issue" + : "错误 curl 无输出 (连接失败?)。这是我们正在排查的 Bug,请报告以便定位: chsrc issue"; println (red (msg)); return -1; } @@ -124,9 +127,12 @@ parse_and_say_curl_result (char *curl_buf) char *split = strchr (curl_buf, ' '); if (NULL==split) { - /* 输出不含空格,格式异常,软处理 */ + /* 输出不含空格,格式异常,软处理;同样请用户报告以便定位正在排查的 Bug */ char *msg = ENGLISH ? "ERROR malformed curl output: " : "错误 curl 输出格式异常: "; - println (red (xy_2strcat (msg, curl_buf))); + char *report = ENGLISH + ? " (This is a bug under investigation, please report: chsrc issue)" + : "(这是我们正在排查的 Bug,请报告以便定位: chsrc issue)"; + println (red (xy_strcat (3, msg, curl_buf, report))); return -1; } From 324f6e577bf9c6efb7e8907c665c37091cd65ebb Mon Sep 17 00:00:00 2001 From: lilyco-42 <251102081+lilyco-42@users.noreply.github.com> Date: Sun, 29 Nov 2026 15:27:29 +0800 Subject: [PATCH 3/3] test: cover malformed curl output and register contributors --- src/chsrc-main.c | 3 +++ src/framework/source.c | 3 ++- test/fw.c | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/chsrc-main.c b/src/chsrc-main.c index ec9679edf..d888f0fd4 100644 --- a/src/chsrc-main.c +++ b/src/chsrc-main.c @@ -93,6 +93,9 @@ chsrc_register_chefs_and_sauciers () chef_register_contributor ("@JIAFALSEDREAM", "假发", "jiafa@hello-happy.world", NULL); chef_register_contributor ("@yayoinoyume", "YAYOI27", "yayoi27@yayoi.uk", NULL); + chef_register_contributor ("@lilyco-42", "Haoyu Zhou", "", "lilyco-42"); + chef_register_contributor ("@YuruiHong", "Yurui Hong", "", "YuruiHong"); + /** * AI贡献者: * diff --git a/src/framework/source.c b/src/framework/source.c index 1068d165d..5239ff800 100644 --- a/src/framework/source.c +++ b/src/framework/source.c @@ -8,9 +8,10 @@ * | @BingChunMoLi * | @Mikachu2333 * | @JIAFALSEDREAM + * | @lilyco-42 * | * Created On : <2023-08-29> - * Last Modified : <2026-08-20> + * Last Modified : <2026-11-29> * * 源的确定 * ------------------------------------------------------------*/ diff --git a/test/fw.c b/test/fw.c index bcceb18b9..c8d6e32ac 100644 --- a/test/fw.c +++ b/test/fw.c @@ -13,6 +13,9 @@ #include "../src/framework/base.c" #include "../src/framework/OS.c" +#include "../src/framework/dish.c" +#include "../src/framework/chef.c" +#include "../src/framework/source.c" int main (int argc, char const *argv[]) @@ -25,6 +28,17 @@ main (int argc, char const *argv[]) xy_init (); + char empty[] = ""; + char malformed[] = "000"; + char failed[] = "000 0"; + char normal[] = "200 1024"; + assert (parse_and_say_curl_result (NULL) == -1); + assert (parse_and_say_curl_result (empty) == -1); + assert (parse_and_say_curl_result (malformed) == -1); + assert (parse_and_say_curl_result (failed) == 0); + assert (parse_and_say_curl_result (normal) == 1024); + + br(); chsrc_log ("chsrc_log"); chsrc_succ ("chsrc_succ");