【问题标题】:Solving and fixing vulnerability pointed by the static analysis tool SPLINT解决和修复静态分析工具 SPLINT 指出的漏洞
【发布时间】:2021-07-02 02:01:44
【问题描述】:

我正在处理我的项目并尝试运行 splint 以查看一些隐藏的漏洞并提高我的代码质量,我在项目的一个 .c 文件上运行了 slint,我遇到了这 4 个警告

Splint 3.1.2 --- 20 Feb 2018

quit_again_final.c: (in function quit)
quit_again_final.c:10:5: Return value (type int) ignored: scanf("%s", ans)
  Result returned by function call is not used. If this is intended, can cast
  result to (void) to eliminate message. (Use -retvalint to inhibit warning)
quit_again_final.c:11:9: Incompatible types for == (char, char):
                            tolower(ans[0]) == 'y'
  A character constant is used as an int. Use +charintliteral to allow
  character constants to be used as ints.  (This is safe since the actual type
  of a char constant is int.)
quit_again_final.c: (in function again)
quit_again_final.c:26:5: Return value (type int) ignored: scanf("%s", ans1)
quit_again_final.c:27:9: Incompatible types for == (char, char):
                            tolower(ans1[0]) == 'y'

Finished checking --- 4 code warnings

我不太确定应该采取哪些步骤来消除漏洞 .c 文件如下所示

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include"server.h"
void quit()
{
    char ans[5];
    printf("ARE YOU SURE YOU WANT TO QUIT THE WIZARD? Y OR N\n");
    scanf("%s", ans);
    if (tolower(ans[0]) == 'y')
    {
        final_print();
        printf("\n\n");
        printf("---THANK YOU FOR USING OUR PORTAL--\n");
    }
    else
    {
        main2();
    }
}
void again()
{
    char ans1[5];
    printf("SEARCH AGAIN USING ID AND PASSWORD? Y OR N\n");
    scanf("%s", ans1);
    if (tolower(ans1[0]) == 'y')
    {
        main2();//FOR RE-LOGIN PROCESS
    }
    else
    {
        quit();
    }
    printf("\n\n\n\n\n\n");
}
void final_print()
{
    printf("----------------FINAL STATEMENT OF LEAVE OF EACH EMPLOYEE------------------------\n\n");
    int i;
    for (i = 0; i < 5; i++)
    {
        printf("NAME : %s\n",emp[i].name);
        printf("USER ID : %s\n",emp[i].id);
        printf("CASUAL LEAVE LEFT : %d\n",emp[i].casual);
        printf("MEDICAL LEAVE LEFT : %d\n",emp[i].medical);
        printf("EARNED LEAVE LEFT : %d\n\n",emp[i].earned);
    }
    printf("---------THANK YOU FOR USING LEAVE MANEGMENT PORTAL-----\n");
}

由于我是处理这些错误的新手,有人可以指导我解决这些错误

错误重复出现,只需解决其中 2 个即可解决整个文件

【问题讨论】:

    标签: c security static-analysis secure-coding splint


    【解决方案1】:

    要消除scanf 的警告,请尝试该工具的建议:

        (void)scanf("%s", ans);
    

    为了消除tolower(ans[0]) == 'y' 的警告,试试这个:

        if (tolower(ans[0]) == (int)'y')
    

    但后者不应该是警告,因为在 C 中,字符文字的类型(例如 'y')是 int,因此 IMO 警告无论如何都是虚假的。

    【讨论】:

    • 虽然将scanf的结果转换为`void`会消除警告,但我建议检查返回值是否是成功转换值的预期数量,例如if (scanf("%s", ans) != 1) { /* handle error */ }
    • 这两个建议都完美无缺,谢谢先生!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 2019-11-13
    • 2012-01-04
    • 2012-02-05
    相关资源
    最近更新 更多