Start caring about the types a little.
[smatch.git] / check_wine_filehandles.c
blob439378365a336e2b52dd4a8c1f8f38bde86e1941
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);
24 /* originally:
25 * "(?:CreateFile|CreateMailslot|CreateNamedPipe|FindFirstFile(?:Ex)?|OpenConsole|SetupOpenInfFile|socket)[AW]?"
28 static const char *filehandle_funcs[] = {
29 "CreateFile",
30 "CreateMailslot",
31 "CreateNamedPipe",
32 "FindFirstFile",
33 "FindFirstFileEx",
34 "OpenConsole",
35 "SetupOpenInfFile",
36 "socket",
37 NULL,
40 static void match_returns_handle(const char *fn, struct expression *expr,
41 void *info)
43 char *left_name = NULL;
44 struct symbol *left_sym;
46 left_name = get_variable_from_expr(expr->left, &left_sym);
47 if (!left_name || !left_sym)
48 goto free;
49 set_state_expr(my_id, expr->left, &filehandle);
50 free:
51 free_string(left_name);
54 static void match_condition(struct expression *expr)
56 if (expr->type == EXPR_ASSIGNMENT)
57 match_condition(expr->left);
59 if (get_state_expr(my_id, expr) == &filehandle) {
60 char *name;
62 name = get_variable_from_expr(expr, NULL);
63 sm_msg("error: comparing a filehandle against zero '%s'", name);
64 set_state_expr(my_id, expr, &oktocheck);
65 free_string(name);
69 void check_wine_filehandles(int id)
71 int i;
73 if (option_project != PROJ_WINE)
74 return;
76 my_id = id;
77 set_default_state(my_id, &oktocheck);
78 for(i = 0; filehandle_funcs[i]; i++) {
79 add_function_assign_hook(filehandle_funcs[i],
80 &match_returns_handle, NULL);
82 add_hook(&match_condition, CONDITION_HOOK);