Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

sysctl: Close test ctl_headers with a for loop

As more tests are added, the exit function gets longer than it should
be. Condense the un-register calls into a for loop to make it easier to
add/remove tests.

Reviewed-by: Kees Cook <kees@kernel.org>
Signed-off-by: Joel Granados <joel.granados@kernel.org>

+29 -36
+29 -36
lib/test_sysctl.c
··· 30 30 static int i_one_hundred = 100; 31 31 static int match_int_ok = 1; 32 32 33 + enum { 34 + TEST_H_SETUP_NODE, 35 + TEST_H_MNT, 36 + TEST_H_MNTERROR, 37 + TEST_H_EMPTY_ADD, 38 + TEST_H_EMPTY, 39 + TEST_H_U8, 40 + TEST_H_SIZE /* Always at the end */ 41 + }; 33 42 34 - static struct { 35 - struct ctl_table_header *test_h_setup_node; 36 - struct ctl_table_header *test_h_mnt; 37 - struct ctl_table_header *test_h_mnterror; 38 - struct ctl_table_header *empty_add; 39 - struct ctl_table_header *empty; 40 - struct ctl_table_header *test_u8; 41 - } sysctl_test_headers; 42 - 43 + static struct ctl_table_header *ctl_headers[TEST_H_SIZE] = {}; 43 44 struct test_sysctl_data { 44 45 int int_0001; 45 46 int int_0002; ··· 169 168 test_data.bitmap_0001 = kzalloc(SYSCTL_TEST_BITMAP_SIZE/8, GFP_KERNEL); 170 169 if (!test_data.bitmap_0001) 171 170 return -ENOMEM; 172 - sysctl_test_headers.test_h_setup_node = register_sysctl("debug/test_sysctl", test_table); 173 - if (!sysctl_test_headers.test_h_setup_node) { 171 + ctl_headers[TEST_H_SETUP_NODE] = register_sysctl("debug/test_sysctl", test_table); 172 + if (!ctl_headers[TEST_H_SETUP_NODE]) { 174 173 kfree(test_data.bitmap_0001); 175 174 return -ENOMEM; 176 175 } ··· 204 203 205 204 static int test_sysctl_run_register_mount_point(void) 206 205 { 207 - sysctl_test_headers.test_h_mnt 206 + ctl_headers[TEST_H_MNT] 208 207 = register_sysctl_mount_point("debug/test_sysctl/mnt"); 209 - if (!sysctl_test_headers.test_h_mnt) 208 + if (!ctl_headers[TEST_H_MNT]) 210 209 return -ENOMEM; 211 210 212 - sysctl_test_headers.test_h_mnterror 211 + ctl_headers[TEST_H_MNTERROR] 213 212 = register_sysctl("debug/test_sysctl/mnt/mnt_error", 214 213 test_table_unregister); 215 214 /* ··· 227 226 static int test_sysctl_run_register_empty(void) 228 227 { 229 228 /* Tets that an empty dir can be created */ 230 - sysctl_test_headers.empty_add 229 + ctl_headers[TEST_H_EMPTY_ADD] 231 230 = register_sysctl("debug/test_sysctl/empty_add", test_table_empty); 232 - if (!sysctl_test_headers.empty_add) 231 + if (!ctl_headers[TEST_H_EMPTY_ADD]) 233 232 return -ENOMEM; 234 233 235 234 /* Test that register on top of an empty dir works */ 236 - sysctl_test_headers.empty 235 + ctl_headers[TEST_H_EMPTY] 237 236 = register_sysctl("debug/test_sysctl/empty_add/empty", test_table_empty); 238 - if (!sysctl_test_headers.empty) 237 + if (!ctl_headers[TEST_H_EMPTY]) 239 238 return -ENOMEM; 240 239 241 240 return 0; ··· 280 279 static int test_sysctl_register_u8_extra(void) 281 280 { 282 281 /* should fail because it's over */ 283 - sysctl_test_headers.test_u8 282 + ctl_headers[TEST_H_U8] 284 283 = register_sysctl("debug/test_sysctl", table_u8_over); 285 - if (sysctl_test_headers.test_u8) 284 + if (ctl_headers[TEST_H_U8]) 286 285 return -ENOMEM; 287 286 288 287 /* should fail because it's under */ 289 - sysctl_test_headers.test_u8 288 + ctl_headers[TEST_H_U8] 290 289 = register_sysctl("debug/test_sysctl", table_u8_under); 291 - if (sysctl_test_headers.test_u8) 290 + if (ctl_headers[TEST_H_U8]) 292 291 return -ENOMEM; 293 292 294 293 /* should not fail because it's valid */ 295 - sysctl_test_headers.test_u8 294 + ctl_headers[TEST_H_U8] 296 295 = register_sysctl("debug/test_sysctl", table_u8_valid); 297 - if (!sysctl_test_headers.test_u8) 296 + if (!ctl_headers[TEST_H_U8]) 298 297 return -ENOMEM; 299 298 300 299 return 0; ··· 322 321 static void __exit test_sysctl_exit(void) 323 322 { 324 323 kfree(test_data.bitmap_0001); 325 - if (sysctl_test_headers.test_h_setup_node) 326 - unregister_sysctl_table(sysctl_test_headers.test_h_setup_node); 327 - if (sysctl_test_headers.test_h_mnt) 328 - unregister_sysctl_table(sysctl_test_headers.test_h_mnt); 329 - if (sysctl_test_headers.test_h_mnterror) 330 - unregister_sysctl_table(sysctl_test_headers.test_h_mnterror); 331 - if (sysctl_test_headers.empty) 332 - unregister_sysctl_table(sysctl_test_headers.empty); 333 - if (sysctl_test_headers.empty_add) 334 - unregister_sysctl_table(sysctl_test_headers.empty_add); 335 - if (sysctl_test_headers.test_u8) 336 - unregister_sysctl_table(sysctl_test_headers.test_u8); 324 + for (int i = 0; i < TEST_H_SIZE; i++) { 325 + if (ctl_headers[i]) 326 + unregister_sysctl_table(ctl_headers[i]); 327 + } 337 328 } 338 329 339 330 module_exit(test_sysctl_exit);