18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/// Correct the size argument to alloc functions 38c2ecf20Sopenharmony_ci/// 48c2ecf20Sopenharmony_ci//# This makes an effort to find cases where the argument to sizeof is wrong 58c2ecf20Sopenharmony_ci//# in memory allocation functions by checking the type of the allocated memory 68c2ecf20Sopenharmony_ci//# when it is a double pointer and ensuring the sizeof argument takes a pointer 78c2ecf20Sopenharmony_ci//# to the the memory being allocated. There are false positives in cases the 88c2ecf20Sopenharmony_ci//# sizeof argument is not used in constructing the return value. The result 98c2ecf20Sopenharmony_ci//# may need some reformatting. 108c2ecf20Sopenharmony_ci// 118c2ecf20Sopenharmony_ci// Confidence: Moderate 128c2ecf20Sopenharmony_ci// Copyright: (C) 2014 Himangi Saraogi. 138c2ecf20Sopenharmony_ci// Comments: 148c2ecf20Sopenharmony_ci// Options: 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_civirtual patch 178c2ecf20Sopenharmony_civirtual context 188c2ecf20Sopenharmony_civirtual org 198c2ecf20Sopenharmony_civirtual report 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci//---------------------------------------------------------- 228c2ecf20Sopenharmony_ci// For context mode 238c2ecf20Sopenharmony_ci//---------------------------------------------------------- 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci@depends on context disable sizeof_type_expr@ 268c2ecf20Sopenharmony_citype T; 278c2ecf20Sopenharmony_ciT **x; 288c2ecf20Sopenharmony_ci@@ 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci x = 318c2ecf20Sopenharmony_ci <+...sizeof( 328c2ecf20Sopenharmony_ci* T 338c2ecf20Sopenharmony_ci )...+> 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci//---------------------------------------------------------- 368c2ecf20Sopenharmony_ci// For patch mode 378c2ecf20Sopenharmony_ci//---------------------------------------------------------- 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci@depends on patch disable sizeof_type_expr@ 408c2ecf20Sopenharmony_citype T; 418c2ecf20Sopenharmony_ciT **x; 428c2ecf20Sopenharmony_ci@@ 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci x = 458c2ecf20Sopenharmony_ci <+...sizeof( 468c2ecf20Sopenharmony_ci- T 478c2ecf20Sopenharmony_ci+ *x 488c2ecf20Sopenharmony_ci )...+> 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci//---------------------------------------------------------- 518c2ecf20Sopenharmony_ci// For org and report mode 528c2ecf20Sopenharmony_ci//---------------------------------------------------------- 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci@r depends on (org || report) disable sizeof_type_expr@ 558c2ecf20Sopenharmony_citype T; 568c2ecf20Sopenharmony_ciT **x; 578c2ecf20Sopenharmony_ciposition p; 588c2ecf20Sopenharmony_ci@@ 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci x = 618c2ecf20Sopenharmony_ci <+...sizeof( 628c2ecf20Sopenharmony_ci T@p 638c2ecf20Sopenharmony_ci )...+> 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci@script:python depends on org@ 668c2ecf20Sopenharmony_cip << r.p; 678c2ecf20Sopenharmony_ci@@ 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_cicoccilib.org.print_todo(p[0], "WARNING sizeof argument should be pointer type, not structure type") 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci@script:python depends on report@ 728c2ecf20Sopenharmony_cip << r.p; 738c2ecf20Sopenharmony_ci@@ 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_cimsg="WARNING: Use correct pointer type argument for sizeof" 768c2ecf20Sopenharmony_cicoccilib.report.print_report(p[0], msg) 778c2ecf20Sopenharmony_ci 78