Avoid triggering a division by zero in the overflow check
[smatch.git] / check_wine_filehandles.c
blobaf710c73509e18ef06b11709ff9283e578be7432
1 /*
2 * sparse/check_wine_filehandles.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * In wine you aren't allowed to compare file handles with 0,
12 * only with INVALID_HANDLE_VALUE.
16 #include "smatch.h"
18 static int my_id;
20 STATE(filehandle);
21 STATE(oktocheck);
23 /* originally:
24 * "(?:CreateFile|CreateMailslot|CreateNamedPipe|FindFirstFile(?:Ex)?|OpenConsole|SetupOpenInfFile|socket)[AW]?"
27 static const char *filehandle_funcs[] = {
28 "CreateFile",
29 "CreateMailslot",
30 "CreateNamedPipe",
31 "FindFirstFile",
32 "FindFirstFileEx",
33 "OpenConsole",
34 "SetupOpenInfFile",
35 "socket",
36 NULL,
39 static void ok_to_use(struct sm_state *sm)
41 if (sm->state != &oktocheck)
42 set_state(my_id, sm->name, sm->sym, &oktocheck);
45 static void match_returns_handle(const char *fn, struct expression *expr,
46 void *info)
48 char *left_name = NULL;
49 struct symbol *left_sym;
51 left_name = expr_to_var_sym(expr->left, &left_sym);
52 if (!left_name || !left_sym)
53 goto free;
54 set_state_expr(my_id, expr->left, &filehandle);
55 free:
56 free_string(left_name);
59 static void match_condition(struct expression *expr)
61 if (expr->type == EXPR_ASSIGNMENT)
62 match_condition(expr->left);
64 if (get_state_expr(my_id, expr) == &filehandle) {
65 char *name;
67 name = expr_to_var(expr);
68 sm_msg("error: comparing a filehandle against zero '%s'", name);
69 set_state_expr(my_id, expr, &oktocheck);
70 free_string(name);
74 void check_wine_filehandles(int id)
76 int i;
78 if (option_project != PROJ_WINE)
79 return;
81 my_id = id;
82 for (i = 0; filehandle_funcs[i]; i++) {
83 add_function_assign_hook(filehandle_funcs[i],
84 &match_returns_handle, NULL);
86 add_hook(&match_condition, CONDITION_HOOK);
87 add_modification_hook(my_id, ok_to_use);