Makefile: small whitespace change
[smatch.git] / check_wine_filehandles.c
blobf22fd60070939d8128cd82867bcb5c8dc50630c3
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(const char *name, struct symbol *sym, struct expression *expr, void *unused)
41 delete_state(my_id, name, sym);
44 static void match_returns_handle(const char *fn, struct expression *expr,
45 void *info)
47 char *left_name = NULL;
48 struct symbol *left_sym;
50 left_name = get_variable_from_expr(expr->left, &left_sym);
51 if (!left_name || !left_sym)
52 goto free;
53 set_state_expr(my_id, expr->left, &filehandle);
54 add_modification_hook_expr(my_id, expr->left, ok_to_use, NULL);
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 = get_variable_from_expr(expr, NULL);
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);