Check return values of sscanf()

Failing to check that a call to 'scanf' actually writes to an output
variable can lead to unexpected behavior at reading time.
pull/1306/head
Mingjie Shen 2023-04-22 18:10:31 -04:00
rodzic 0a5cad7ee8
commit 8f97e62708
2 zmienionych plików z 8 dodań i 3 usunięć

Wyświetl plik

@ -160,8 +160,10 @@ int parse_options(int argc, char** argv, st_state_t *st) {
break;
case 'p':
sscanf(optarg, "%i", &q);
if (q < 0) {
if (sscanf(optarg, "%i", &q) != 1) {
fprintf(stderr, "Invalid port %s\n", optarg);
exit(EXIT_FAILURE);
} else if (q < 0) {
fprintf(stderr, "Can't use a negative port to listen on: %d\n", q);
exit(EXIT_FAILURE);
}

Wyświetl plik

@ -64,7 +64,10 @@ void process_chipfile(char *fname) {
(strncmp(buf, " ", strlen(" ")) == 0))
continue; // ignore empty lines
sscanf(buf, "%63s %63s", word, value);
if (sscanf(buf, "%63s %63s", word, value) != 2) {
fprintf(stderr, "Failed to read keyword or value\n");
continue;
}
if (strcmp(word, "dev_type") == 0) {
buf[strlen(buf) - 1] = 0; // chomp newline