Skip to content

Fix bug when './chsrc ls ruby'#347

Open
Gchuchu wants to merge 3 commits intodevfrom
fix_bug_ls
Open

Fix bug when './chsrc ls ruby'#347
Gchuchu wants to merge 3 commits intodevfrom
fix_bug_ls

Conversation

@Gchuchu
Copy link
Collaborator

@Gchuchu Gchuchu commented Mar 18, 2026

问题描述

简要说明此 PR 修复的具体问题

  1. 解决了./chsrc ls ruby出现的段错误问题
可用功能:

 ✓ Get: 查看当前源状态 | chsrc get ruby

 ✓ Reset: 重置回默认源 | chsrc reset ruby

 ✓ UserDefine: 用户自定义换源链接 | chsrc set ruby https://user-define-url.org/ruby

 ✓ 项目级换源 | chsrc set -scope=project ruby (支持且已实现)
 ✓ 用户级换源 | chsrc set -scope=user    ruby (支持且已实现)
 x 系统级换源 | chsrc set -scope=system  ruby (不支持)
段错误 (核心已转储)

问题原因:cli_print_target_features函数中xy_strcat写的时候计数错误(2错写成了3)

  1. 解决了导致的编译错误问题

#344 遗留问题

gcc版本:

# gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0

makemake debug都编译出错:

 # make debug
Starting: Build in DEBUG mode: 'cc' -Iinclude -Ilib -g -DXY_DEBUG -o chsrc-debug
In file included from src/framework/core.c:27,
                 from src/chsrc-main.c:37:
lib/xy.h: In function ‘_xy_str_to_terminal_style’:
lib/xy.h:514:3: error: a label can only be part of a statement and a declaration is not a statement
  514 |   size_t len = strlen (color_fmt_str) - 2;
      |   ^~~~~~
lib/xy.h:515:3: error: expected expression before ‘char’
  515 |   char *buf = malloc (strlen (str) + len + 1);
      |   ^~~~
lib/xy.h:516:12: error: ‘buf’ undeclared (first use in this function)
  516 |   sprintf (buf, color_fmt_str, str);
      |            ^~~
lib/xy.h:516:12: note: each undeclared identifier is reported only once for each function it appears in
In file included from src/framework/core.c:27,
                 from src/chsrc-main.c:37:
lib/xy.h:518:1: warning: control reaches end of non-void function [-Wreturn-type]
  518 | }
      | ^

原因
C99标准文档6.8.1Labeled statements中有描述
label后第一句必须是statement,而不是一个declaration。这种不会出错,例如:

label_name: x = 1;          
label_name: return 0;       
label_name: if (a) b();     
label_name: { int x; }     
label_name: ;               
label_name: goto other;     

而这种会出错:

label_name: int x = 0;      
label_name: char *p;        

方案与实现

详细描述针对该问题或功能改进的解决方案

  1. 修改xy_strcat的参数
  2. 将定义提前,使得标签后第一句为statement

@github-actions
Copy link

Hi @Gchuchu

❤️ 感谢你的贡献!我们将在最少半小时,最多5天内阅读此 PR 并回复你

Copy link
Contributor

@ccmywish ccmywish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍👍

label后第一句必须是statement,而不是一个declaration

这个问题我也遇到了,所以最开始的原代码就是写在label上面一行的。#344 又由AI改成错的了。而 #344 由于 force push了代码所以又跳过了在 actions 上测试的机会。

我建议把这个声明放在标签上一行,然后给一个简单的注释

@ccmywish ccmywish added this to the v0.2.5 milestone Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants