Skip to content
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

导出函数命令参数解析bug #154

Open
Blueknife-w opened this issue Apr 25, 2023 · 1 comment
Open

导出函数命令参数解析bug #154

Blueknife-w opened this issue Apr 25, 2023 · 1 comment

Comments

@Blueknife-w
Copy link

在使用letter shell的函数导出功能过程中,在以下两个情景遇到缺陷:


  1. 导出函数输入参数为字符串,如果实际传入的为纯数字,且未使用""标记,会导致崩溃
int set_machine_sn(char *sn_str, char check)
{
    int len = 0;

    len = strlen(sn_str);
    if (len != 15) {
        ESP_LOGW(TAG, "machine_sn length error!\n");
        ret = -1;
        goto error;
    }
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC), set_machine_sn, set_machine_sn, Set machine sn vale);

如果在终端输入 set_machine_sn 12345678 13 命令以后崩溃,报以下错误:

Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.

Core  1 register dump:
PC      : 0x400014e8  PS      : 0x00060430  A0      : 0x800de739  A1      : 0x3ffd1720  
A2      : 0x00bc614e  A3      : 0x00bc614c  A4      : 0x000000ff  A5      : 0x0000ff00  
A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x00000000  A9      : 0x3ffd1700  
A10     : 0x00000000  A11     : 0x00000000  A12     : 0x3ffd06ac  A13     : 0x00000000  
A14     : 0x00000000  A15     : 0x0000abab  SAR     : 0x00000010  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00bc614c  LBEG    : 0x400012e5  LEND    : 0x40001309  LCOUNT  : 0x800da587  


Backtrace: 0x400014e5:0x3ffd1720 0x400de736:0x3ffd1730 0x400daf11:0x3ffd1760 0x400daa88:0x3ffd17c0 0x400dab4d:0x3ffd17e0 0x400dab6e:0x3ffd1800 0x400dac56:0x3ffd1820 0x400dacab:0x3ffd1850 0x4008d1e5:0x3ffd1880
0x400de736: set_machine_sn at /home/test/build/../user/app/sn.c:25

  1. 导出函数输入参数为字符串,在命令使用时如果不传入参数,传入给函数的参数不为NULL,也不知道是什么值,而在函数内如果对此参数进行操作,则会导致崩溃
int scan_wifi(char *ssid)
{
    int ret = 0;

    if (ssid == NULL) {
        printf("ssid is NULL!");
    }
    printf("ssid:%p\n", ssid);
    printf("ssid:%c\n", ssid[0]);
    return ret;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC), scan_wifi, scan_wifi, scan wifi);

终端输入 scan_wifi 之后回车,程序崩溃,日志如下:

ssid:0x400e1dbcfi
0x400e1dbc: scan_wifi at /home/test/build/../user/app/shell_cmd.c:210

Guru Meditation Error: Core  0 panic'ed (LoadStoreError). Exception was unhandled.
@NevermindZZT
Copy link
Owner

因为参数自动解析是根据输入进行猜测的,所以会出现这个问题

第一个情况,参数输入如果是数字,会按照数字去解析,如果要作为字符串处理,需要加双引号 set_machine_sn "12345678" 13

第二个情况,可以声明一个参数数量,这样子如果输入的参数不够,会以 0 值补充参数

SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(1), scan_wifi, scan_wifi, scan wifi);

另外,shell3.1 分支刚刚增加了函数签名的新功能,可以完美解决这两种情况,但是这个功能刚刚加入,还没有合入 master, 如果感兴趣,可以使用这个分支使用新功能解决这两个问题

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

No branches or pull requests

2 participants