Page 1 of 1

Potential issues/sugesstions in zm_config.cpp

Posted: Sun Jan 04, 2004 1:38 am
by snakebyte
On line 52:
cfg_value.boolean_value = strtol( value, 0, 2 );

boolean_value will incorrectly come up as TRUE if value equals "-1".

possible fix:
long tmp=strtol( value, 0, 2 );
if ( (1 == tmp) )
cfg_value.boolean_value = true;
else
cfg_value.boolean_value = false;

On line 67:
cfg_value.decimal_value = atof( value );

suggest using strtod instead of atof because of the min/max boundry protection:
cfg_value.decimal_value = strtod( value, 0 );

On line 154:
n_items = mysql_num_rows( result );

Depending, mysql_num_rows can return anything from an unsigned long to an unsigned int64. n_items will incorrectly go negative if anything above 0x7FFFFFFF is returned. Of course, the chances of mysql returning something that large is remote, but I thought I'd bring this up.

Possible fix:
n_items = mysql_num_rows( result );
if (0 > n_items)
{
n_items=0x7FFFFFFF;
// internal capacity limit warning message?
}

On lines 156:
for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ )

Assignment is happening in a conditional (which is fine) but to keep the compiler from issuing a warning at the higher warning levels:

MYSQL_ROW dbrow;
for( int i = 0; 0 != (dbrow = mysql_fetch_row( result )); i++ )


Here's a diff of the changes:

52c52,56
< cfg_value.boolean_value = strtol( value, 0, 2 );
---
> long tmp=strtol( value, 0, 2 );
> if ( (1 == tmp) )
> cfg_value.boolean_value = true;
> else
> cfg_value.boolean_value = false;
67c71
< cfg_value.decimal_value = atof( value );
---
> cfg_value.decimal_value = strtod( value, 0 );
154a159,163
> if (0 > n_items)
> {
> n_items=0x7FFFFFFF;
> // internal capacity limit warning message?
> }
156c165,166
< for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ )
---
> MYSQL_ROW dbrow;
> for( int i = 0; 0 != (dbrow = mysql_fetch_row( result )); i++ )