Line data Source code
1 : #include "fd_config_extract.h" 2 : 3 : /* Find leftover ******************************************************/ 4 : 5 : /* fdctl_pod_find_leftover recursively searches for non-subpod keys in 6 : pod. Prints to the warning log if it finds any. Used to detect 7 : config keys that were not recognized by fdctl. Returns 0 if no 8 : leftover key was found. Otherwise, returns a non-zero number of 9 : segments of the leftover key. The key can be reassembled by joining 10 : stack[0] .. stack[depth-1]. 11 : 12 : Not thread safe (uses global buffer). */ 13 : 14 6 : # define FDCTL_CFG_MAX_DEPTH (16) 15 : 16 : static ulong 17 : fdctl_pod_find_leftover_recurse( uchar * pod, 18 : char const ** stack, 19 438 : ulong depth ) { 20 : 21 438 : if( FD_UNLIKELY( depth+1 >= FDCTL_CFG_MAX_DEPTH ) ) { 22 0 : FD_LOG_WARNING(( "configuration file has too many nested keys" )); 23 0 : return depth; 24 0 : } 25 : 26 840 : for( fd_pod_iter_t iter = fd_pod_iter_init( pod ); !fd_pod_iter_done( iter ); iter = fd_pod_iter_next( iter ) ) { 27 408 : fd_pod_info_t info = fd_pod_iter_info( iter ); 28 408 : stack[ depth ] = info.key; 29 408 : depth++; 30 408 : if( FD_LIKELY( info.val_type == FD_POD_VAL_TYPE_SUBPOD ) ) { 31 402 : ulong sub_depth = fdctl_pod_find_leftover_recurse( (uchar *)info.val, stack, depth ); 32 402 : if( FD_UNLIKELY( sub_depth ) ) return sub_depth; 33 402 : } else { 34 6 : return depth; 35 6 : } 36 402 : depth--; 37 402 : } 38 : 39 432 : return 0; 40 438 : } 41 : 42 : int 43 36 : fdctl_pod_find_leftover( uchar * pod ) { 44 : 45 36 : static char const * stack[ FDCTL_CFG_MAX_DEPTH ]; 46 36 : ulong depth = fdctl_pod_find_leftover_recurse( pod, stack, 0UL ); 47 36 : if( FD_LIKELY( !depth ) ) return 1; 48 : 49 6 : static char path[ 64*FDCTL_CFG_MAX_DEPTH + 4 ]; 50 6 : char * c = fd_cstr_init( path ); 51 6 : char * end = path + 64*FDCTL_CFG_MAX_DEPTH - 1; 52 12 : for( ulong j=0UL; j<depth; j++ ) { 53 6 : char const * key = stack[j]; 54 6 : ulong key_len = strlen( key ); 55 6 : if( c+key_len+1 >= end ) { 56 0 : c = fd_cstr_append_text( c, "...", 3UL ); 57 0 : break; 58 0 : } 59 6 : c = fd_cstr_append_text( c, key, key_len ); 60 6 : c = fd_cstr_append_char( c, '.' ); 61 6 : } 62 6 : c -= 1; 63 6 : fd_cstr_fini( c ); 64 : 65 6 : FD_LOG_WARNING(( "Config file contains unrecognized key `%s`", path )); 66 6 : return 0; 67 36 : }