csecuritystatic-analysissecure-codingsplint

Solving and fixing vulnerability pointed by the static analysis tool SPLINT


I was working on my project and tried to run splint to see some hidden vulnerability and improve the my quality of code and I ran splint on one of my .c files of the project and I came across these 4 warnings

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

I am not quite sure about the steps I should take to eliminate vulnerabilities The .c file is given below

#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");
}

Can someone please guide me with these errors as I am a newbie to handling these errors

THE ERRORS ARE REPEATED AND JUST SOLVING 2 OF THEM WILL RESOLVE THE ENTIRE FILE


Solution

  • For eliminating the warning for scanf try what the tool suggests:

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

    For eliminating the warning for tolower(ans[0]) == 'y' try this:

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

    But latter shouldn't be a warning because in C the type of a char literal (such as 'y') is int, so IMO the warning is bogous anyway.