function_hooks: update comment explaining various function hooks
[smatch.git] / check_wine_filehandles.c
blobc4bf8a7aa17ae35c4b5a2fcff1b4dce99f673f95
1 /*
2 * Copyright (C) 2009 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
19 * In wine you aren't allowed to compare file handles with 0,
20 * only with INVALID_HANDLE_VALUE.
24 #include "smatch.h"
26 static int my_id;
28 STATE(filehandle);
29 STATE(oktocheck);
31 /* originally:
32 * "(?:CreateFile|CreateMailslot|CreateNamedPipe|FindFirstFile(?:Ex)?|OpenConsole|SetupOpenInfFile|socket)[AW]?"
35 static const char *filehandle_funcs[] = {
36 "CreateFile",
37 "CreateMailslot",
38 "CreateNamedPipe",
39 "FindFirstFile",
40 "FindFirstFileEx",
41 "OpenConsole",
42 "SetupOpenInfFile",
43 "socket",
44 NULL,
47 static void ok_to_use(struct sm_state *sm, struct expression *mod_expr)
49 if (sm->state != &oktocheck)
50 set_state(my_id, sm->name, sm->sym, &oktocheck);
53 static void match_returns_handle(const char *fn, struct expression *expr,
54 void *info)
56 char *left_name = NULL;
57 struct symbol *left_sym;
59 left_name = expr_to_var_sym(expr->left, &left_sym);
60 if (!left_name || !left_sym)
61 goto free;
62 set_state_expr(my_id, expr->left, &filehandle);
63 free:
64 free_string(left_name);
67 static void match_condition(struct expression *expr)
69 if (expr->type == EXPR_ASSIGNMENT)
70 match_condition(expr->left);
72 if (get_state_expr(my_id, expr) == &filehandle) {
73 char *name;
75 name = expr_to_var(expr);
76 sm_error("comparing a filehandle against zero '%s'", name);
77 set_state_expr(my_id, expr, &oktocheck);
78 free_string(name);
82 void check_wine_filehandles(int id)
84 int i;
86 if (option_project != PROJ_WINE)
87 return;
89 my_id = id;
90 for (i = 0; filehandle_funcs[i]; i++) {
91 add_function_assign_hook(filehandle_funcs[i],
92 &match_returns_handle, NULL);
94 add_hook(&match_condition, CONDITION_HOOK);
95 add_modification_hook(my_id, ok_to_use);