1919#include <linux/kmemleak.h>
2020#include "internal.h"
2121
22+ #define list_for_each_table_entry (entry , table ) \
23+ for ((entry) = (table); (entry)->procname; (entry)++)
24+
2225static const struct dentry_operations proc_sys_dentry_operations ;
2326static const struct file_operations proc_sys_file_operations ;
2427static const struct inode_operations proc_sys_inode_operations ;
@@ -215,15 +218,19 @@ static void init_header(struct ctl_table_header *head,
215218 INIT_HLIST_HEAD (& head -> inodes );
216219 if (node ) {
217220 struct ctl_table * entry ;
218- for (entry = table ; entry -> procname ; entry ++ , node ++ )
221+
222+ list_for_each_table_entry (entry , table ) {
219223 node -> header = head ;
224+ node ++ ;
225+ }
220226 }
221227}
222228
223229static void erase_header (struct ctl_table_header * head )
224230{
225231 struct ctl_table * entry ;
226- for (entry = head -> ctl_table ; entry -> procname ; entry ++ )
232+
233+ list_for_each_table_entry (entry , head -> ctl_table )
227234 erase_entry (head , entry );
228235}
229236
@@ -248,7 +255,7 @@ static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
248255 err = insert_links (header );
249256 if (err )
250257 goto fail_links ;
251- for (entry = header -> ctl_table ; entry -> procname ; entry ++ ) {
258+ list_for_each_table_entry (entry , header -> ctl_table ) {
252259 err = insert_entry (header , entry );
253260 if (err )
254261 goto fail ;
@@ -978,7 +985,6 @@ static struct ctl_dir *new_dir(struct ctl_table_set *set,
978985 table = (struct ctl_table * )(node + 1 );
979986 new_name = (char * )(table + 2 );
980987 memcpy (new_name , name , namelen );
981- new_name [namelen ] = '\0' ;
982988 table [0 ].procname = new_name ;
983989 table [0 ].mode = S_IFDIR |S_IRUGO |S_IXUGO ;
984990 init_header (& new -> header , set -> dir .header .root , set , node , table );
@@ -1130,35 +1136,36 @@ static int sysctl_check_table_array(const char *path, struct ctl_table *table)
11301136
11311137static int sysctl_check_table (const char * path , struct ctl_table * table )
11321138{
1139+ struct ctl_table * entry ;
11331140 int err = 0 ;
1134- for (; table -> procname ; table ++ ) {
1135- if (table -> child )
1136- err |= sysctl_err (path , table , "Not a file" );
1137-
1138- if ((table -> proc_handler == proc_dostring ) ||
1139- (table -> proc_handler == proc_dointvec ) ||
1140- (table -> proc_handler == proc_douintvec ) ||
1141- (table -> proc_handler == proc_douintvec_minmax ) ||
1142- (table -> proc_handler == proc_dointvec_minmax ) ||
1143- (table -> proc_handler == proc_dou8vec_minmax ) ||
1144- (table -> proc_handler == proc_dointvec_jiffies ) ||
1145- (table -> proc_handler == proc_dointvec_userhz_jiffies ) ||
1146- (table -> proc_handler == proc_dointvec_ms_jiffies ) ||
1147- (table -> proc_handler == proc_doulongvec_minmax ) ||
1148- (table -> proc_handler == proc_doulongvec_ms_jiffies_minmax )) {
1149- if (!table -> data )
1150- err |= sysctl_err (path , table , "No data" );
1151- if (!table -> maxlen )
1152- err |= sysctl_err (path , table , "No maxlen" );
1141+ list_for_each_table_entry ( entry , table ) {
1142+ if (entry -> child )
1143+ err |= sysctl_err (path , entry , "Not a file" );
1144+
1145+ if ((entry -> proc_handler == proc_dostring ) ||
1146+ (entry -> proc_handler == proc_dointvec ) ||
1147+ (entry -> proc_handler == proc_douintvec ) ||
1148+ (entry -> proc_handler == proc_douintvec_minmax ) ||
1149+ (entry -> proc_handler == proc_dointvec_minmax ) ||
1150+ (entry -> proc_handler == proc_dou8vec_minmax ) ||
1151+ (entry -> proc_handler == proc_dointvec_jiffies ) ||
1152+ (entry -> proc_handler == proc_dointvec_userhz_jiffies ) ||
1153+ (entry -> proc_handler == proc_dointvec_ms_jiffies ) ||
1154+ (entry -> proc_handler == proc_doulongvec_minmax ) ||
1155+ (entry -> proc_handler == proc_doulongvec_ms_jiffies_minmax )) {
1156+ if (!entry -> data )
1157+ err |= sysctl_err (path , entry , "No data" );
1158+ if (!entry -> maxlen )
1159+ err |= sysctl_err (path , entry , "No maxlen" );
11531160 else
1154- err |= sysctl_check_table_array (path , table );
1161+ err |= sysctl_check_table_array (path , entry );
11551162 }
1156- if (!table -> proc_handler )
1157- err |= sysctl_err (path , table , "No proc_handler" );
1163+ if (!entry -> proc_handler )
1164+ err |= sysctl_err (path , entry , "No proc_handler" );
11581165
1159- if ((table -> mode & (S_IRUGO |S_IWUGO )) != table -> mode )
1160- err |= sysctl_err (path , table , "bogus .mode 0%o" ,
1161- table -> mode );
1166+ if ((entry -> mode & (S_IRUGO |S_IWUGO )) != entry -> mode )
1167+ err |= sysctl_err (path , entry , "bogus .mode 0%o" ,
1168+ entry -> mode );
11621169 }
11631170 return err ;
11641171}
@@ -1174,7 +1181,7 @@ static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table
11741181
11751182 name_bytes = 0 ;
11761183 nr_entries = 0 ;
1177- for (entry = table ; entry -> procname ; entry ++ ) {
1184+ list_for_each_table_entry (entry , table ) {
11781185 nr_entries ++ ;
11791186 name_bytes += strlen (entry -> procname ) + 1 ;
11801187 }
@@ -1191,14 +1198,16 @@ static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table
11911198 node = (struct ctl_node * )(links + 1 );
11921199 link_table = (struct ctl_table * )(node + nr_entries );
11931200 link_name = (char * )& link_table [nr_entries + 1 ];
1201+ link = link_table ;
11941202
1195- for ( link = link_table , entry = table ; entry -> procname ; link ++ , entry ++ ) {
1203+ list_for_each_table_entry ( entry , table ) {
11961204 int len = strlen (entry -> procname ) + 1 ;
11971205 memcpy (link_name , entry -> procname , len );
11981206 link -> procname = link_name ;
11991207 link -> mode = S_IFLNK |S_IRWXUGO ;
12001208 link -> data = link_root ;
12011209 link_name += len ;
1210+ link ++ ;
12021211 }
12031212 init_header (links , dir -> header .root , dir -> header .set , node , link_table );
12041213 links -> nreg = nr_entries ;
@@ -1213,7 +1222,7 @@ static bool get_links(struct ctl_dir *dir,
12131222 struct ctl_table * entry , * link ;
12141223
12151224 /* Are there links available for every entry in table? */
1216- for (entry = table ; entry -> procname ; entry ++ ) {
1225+ list_for_each_table_entry (entry , table ) {
12171226 const char * procname = entry -> procname ;
12181227 link = find_entry (& head , dir , procname , strlen (procname ));
12191228 if (!link )
@@ -1226,7 +1235,7 @@ static bool get_links(struct ctl_dir *dir,
12261235 }
12271236
12281237 /* The checks passed. Increase the registration count on the links */
1229- for (entry = table ; entry -> procname ; entry ++ ) {
1238+ list_for_each_table_entry (entry , table ) {
12301239 const char * procname = entry -> procname ;
12311240 link = find_entry (& head , dir , procname , strlen (procname ));
12321241 head -> nreg ++ ;
@@ -1329,7 +1338,7 @@ struct ctl_table_header *__register_sysctl_table(
13291338 struct ctl_node * node ;
13301339 int nr_entries = 0 ;
13311340
1332- for (entry = table ; entry -> procname ; entry ++ )
1341+ list_for_each_table_entry (entry , table )
13331342 nr_entries ++ ;
13341343
13351344 header = kzalloc (sizeof (struct ctl_table_header ) +
@@ -1456,7 +1465,7 @@ static int count_subheaders(struct ctl_table *table)
14561465 if (!table || !table -> procname )
14571466 return 1 ;
14581467
1459- for (entry = table ; entry -> procname ; entry ++ ) {
1468+ list_for_each_table_entry (entry , table ) {
14601469 if (entry -> child )
14611470 nr_subheaders += count_subheaders (entry -> child );
14621471 else
@@ -1475,7 +1484,7 @@ static int register_leaf_sysctl_tables(const char *path, char *pos,
14751484 int nr_dirs = 0 ;
14761485 int err = - ENOMEM ;
14771486
1478- for (entry = table ; entry -> procname ; entry ++ ) {
1487+ list_for_each_table_entry (entry , table ) {
14791488 if (entry -> child )
14801489 nr_dirs ++ ;
14811490 else
@@ -1492,7 +1501,9 @@ static int register_leaf_sysctl_tables(const char *path, char *pos,
14921501 goto out ;
14931502
14941503 ctl_table_arg = files ;
1495- for (new = files , entry = table ; entry -> procname ; entry ++ ) {
1504+ new = files ;
1505+
1506+ list_for_each_table_entry (entry , table ) {
14961507 if (entry -> child )
14971508 continue ;
14981509 * new = * entry ;
@@ -1516,7 +1527,7 @@ static int register_leaf_sysctl_tables(const char *path, char *pos,
15161527 }
15171528
15181529 /* Recurse into the subdirectories. */
1519- for (entry = table ; entry -> procname ; entry ++ ) {
1530+ list_for_each_table_entry (entry , table ) {
15201531 char * child_pos ;
15211532
15221533 if (!entry -> child )
@@ -1670,7 +1681,7 @@ static void put_links(struct ctl_table_header *header)
16701681 if (IS_ERR (core_parent ))
16711682 return ;
16721683
1673- for (entry = header -> ctl_table ; entry -> procname ; entry ++ ) {
1684+ list_for_each_table_entry (entry , header -> ctl_table ) {
16741685 struct ctl_table_header * link_head ;
16751686 struct ctl_table * link ;
16761687 const char * name = entry -> procname ;
0 commit comments