math: cleanup: make handling binops a switch() statement
[smatch.git] / check_wine_filehandles.c
blobdd48bcde554ad2d3058a919b8270b3a538ae5c00
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 ok_to_use(const char *name, struct symbol *sym, struct expression *expr, void *unused)
42 delete_state(my_id, name, sym);
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 = get_variable_from_expr(expr->left, &left_sym);
52 if (!left_name || !left_sym)
53 goto free;
54 set_state_expr(my_id, expr->left, &filehandle);
55 add_modification_hook_expr(my_id, expr->left, ok_to_use, NULL);
56 free:
57 free_string(left_name);
60 static void match_condition(struct expression *expr)
62 if (expr->type == EXPR_ASSIGNMENT)
63 match_condition(expr->left);
65 if (get_state_expr(my_id, expr) == &filehandle) {
66 char *name;
68 name = get_variable_from_expr(expr, NULL);
69 sm_msg("error: comparing a filehandle against zero '%s'", name);
70 set_state_expr(my_id, expr, &oktocheck);
71 free_string(name);
75 void check_wine_filehandles(int id)
77 int i;
79 if (option_project != PROJ_WINE)
80 return;
82 my_id = id;
83 for(i = 0; filehandle_funcs[i]; i++) {
84 add_function_assign_hook(filehandle_funcs[i],
85 &match_returns_handle, NULL);
87 add_hook(&match_condition, CONDITION_HOOK);