Skip to main content

IAR Embedded Workbench for RH850 3.20.x

MISRAC2012-Dir-4.14_c

In this section:
Synopsis

(Required) The validity of values received from external sources shall be checked.

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

User input is copied into a buffer.

Coding standards
MISRA C:2012 Dir-4.14

(Required) The validity of values received from external sources shall be checked

Code examples

The following code example fails the check and will give a warning:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
  char passwd[10];
  char *input = getenv("PASSWORD");
  int accept;
  
  strcpy(passwd, input);

  if (accept)
    printf("Login Successful\n");
  else
    printf("Unsuccessful Login\n");
}

The following code example passes the check and will not give a warning about this issue:

#include <string.h>
#include <stdio.h>

int main(int argc, char **argv) {
  char passwd[10];
  int accept;
  
  if (strlen(argv[1]) < 10)
    strcpy(passwd, argv[1]);

  if (accept)
    printf("Login Successful\n");
  else
    printf("Unsuccessful Login\n");
}