LCOV - code coverage report
Current view: top level - app/platform - fd_config_extract.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 42 95 44.2 %
Date: 2025-07-01 05:00:49 Functions: 5 12 41.7 %

          Line data    Source code
       1             : #include "../../util/fd_util.h"
       2             : #include "../../util/pod/fd_pod.h"
       3             : 
       4             : FD_PROTOTYPES_BEGIN
       5             : 
       6             : /* fdctl_pod_find_leftover recursively descends a pod and logs a
       7             :    WARNING for each remaining leaf item.  Intended for warning the user
       8             :    about unrecognized config options, after loading config files into a
       9             :    pod. */
      10             : 
      11             : int
      12             : fdctl_pod_find_leftover( uchar * pod );
      13             : 
      14             : /* Pod query utils ****************************************************/
      15             : 
      16             : static inline int
      17             : fdctl_cfg_get_cstr_( char *                out,
      18             :                      ulong                 out_sz,
      19             :                      fd_pod_info_t const * info,
      20         156 :                      char const *          path ) {
      21         156 :   if( FD_UNLIKELY( info->val_type != FD_POD_VAL_TYPE_CSTR ) ) {
      22           0 :     FD_LOG_WARNING(( "invalid value for `%s`", path ));
      23           0 :     return 0;
      24           0 :   }
      25         156 :   char const * str = info->val;
      26         156 :   ulong        sz  = strlen( str ) + 1;
      27         156 :   if( FD_UNLIKELY( sz > out_sz ) ) {
      28           0 :     FD_LOG_WARNING(( "`%s`: too long (max %ld)", path, (long)out_sz-1L ));
      29           0 :     return 0;
      30           0 :   }
      31         156 :   fd_memcpy( out, str, sz );
      32         156 :   return 1;
      33         156 : }
      34             : 
      35             : #define fdctl_cfg_get_cstr( out, out_sz, info, path ) \
      36           6 :   fdctl_cfg_get_cstr_( *out, out_sz, info, path )
      37             : 
      38             : static inline int
      39             : fdctl_cfg_get_ulong( ulong *               out,
      40             :                      ulong                 out_sz FD_PARAM_UNUSED,
      41             :                      fd_pod_info_t const * info,
      42         177 :                      char const *          path ) {
      43             : 
      44         177 :   ulong num;
      45         177 :   switch( info->val_type ) {
      46         177 :   case FD_POD_VAL_TYPE_LONG:
      47         177 :     fd_ulong_svw_dec( (uchar const *)info->val, &num );
      48         177 :     long snum = fd_long_zz_dec( num );
      49         177 :     if( snum < 0L ) {
      50           0 :       FD_LOG_WARNING(( "`%s` cannot be negative", path ));
      51           0 :       return 0;
      52           0 :     }
      53         177 :     num = (ulong)snum;
      54         177 :     break;
      55           0 :   case FD_POD_VAL_TYPE_ULONG:
      56           0 :     fd_ulong_svw_dec( (uchar const *)info->val, &num );
      57           0 :     break;
      58           0 :   default:
      59           0 :     FD_LOG_WARNING(( "invalid value for `%s`", path ));
      60           0 :     return 0;
      61         177 :   }
      62             : 
      63         177 :   *out = num;
      64         177 :   return 1;
      65         177 : }
      66             : 
      67             : static inline int
      68             : fdctl_cfg_get_uint( uint *                out,
      69             :                     ulong                 out_sz FD_PARAM_UNUSED,
      70             :                     fd_pod_info_t const * info,
      71         108 :                     char const *          path ) {
      72         108 :   ulong num;
      73         108 :   if( FD_UNLIKELY( !fdctl_cfg_get_ulong( &num, sizeof(num), info, path ) ) ) return 0;
      74         108 :   if( num > UINT_MAX ) {
      75           0 :     FD_LOG_WARNING(( "`%s` is out of bounds (%lx)", path, num ));
      76           0 :     return 0;
      77           0 :   }
      78         108 :   *out = (uint)num;
      79         108 :   return 1;
      80         108 : }
      81             : 
      82             : static inline int
      83             : fdctl_cfg_get_ushort( ushort *              out,
      84             :                       ulong                 out_sz FD_PARAM_UNUSED,
      85             :                       fd_pod_info_t const * info,
      86          24 :                       char const *          path ) {
      87          24 :   ulong num;
      88          24 :   if( FD_UNLIKELY( !fdctl_cfg_get_ulong( &num, sizeof(num), info, path ) ) ) return 0;
      89          24 :   if( num > USHORT_MAX ) {
      90           0 :     FD_LOG_WARNING(( "`%s` is out of bounds (%lx)", path, num ));
      91           0 :     return 0;
      92           0 :   }
      93          24 :   *out = (ushort)num;
      94          24 :   return 1;
      95          24 : }
      96             : 
      97             : static inline int
      98             : fdctl_cfg_get_bool( int *                 out,
      99             :                     ulong                 out_sz FD_PARAM_UNUSED,
     100             :                     fd_pod_info_t const * info,
     101         108 :                     char const *          path ) {
     102         108 :   if( FD_UNLIKELY( info->val_type != FD_POD_VAL_TYPE_INT ) ) {
     103           0 :     FD_LOG_WARNING(( "invalid value for `%s`", path ));
     104           0 :     return 0;
     105           0 :   }
     106         108 :   ulong u; fd_ulong_svw_dec( (uchar const *)info->val, &u );
     107         108 :   *out = fd_int_zz_dec( (uint)u );
     108         108 :   return 1;
     109         108 : }
     110             : 
     111             : static inline int
     112             : fdctl_cfg_get_float( float *               out,
     113             :                      ulong                 out_sz FD_PARAM_UNUSED,
     114             :                      fd_pod_info_t const * info,
     115           0 :                      char const *          path ) {
     116           0 : 
     117           0 :   ulong unum;
     118           0 :   float num;
     119           0 :   switch( info->val_type ) {
     120           0 :   case FD_POD_VAL_TYPE_LONG:
     121           0 :     fd_ulong_svw_dec( (uchar const *)info->val, &unum );
     122           0 :     long snum = fd_long_zz_dec( unum );
     123           0 :     if( snum < 0L ) {
     124           0 :       FD_LOG_WARNING(( "`%s` cannot be negative", path ));
     125           0 :       return 0;
     126           0 :     }
     127           0 :     num = (float)snum;
     128           0 :     break;
     129           0 :   case FD_POD_VAL_TYPE_ULONG:
     130           0 :     fd_ulong_svw_dec( (uchar const *)info->val, &unum );
     131           0 :     num = (float)unum;
     132           0 :     break;
     133           0 :   case FD_POD_VAL_TYPE_FLOAT:
     134           0 :     num = FD_LOAD( float, info->val );
     135           0 :     break;
     136           0 :   default:
     137           0 :     FD_LOG_WARNING(( "invalid value for `%s`", path ));
     138           0 :     return 0;
     139           0 :   }
     140           0 : 
     141           0 :   *out = num;
     142           0 :   return 1;
     143           0 : }
     144             : 
     145             : FD_PROTOTYPES_END

Generated by: LCOV version 1.14