变量 (a, r, g, b) 将包含早期计算留下的任意值。
unsigned int parse_colour( const char *str )
{
unsigned int a, r, g, b;
int ret;
if( !str || !*str ) return 0;
if( strlen( str ) == 1 ) return (unsigned int)atoi( str );
if( str[0] == '0' && str[1] == 'x' ) {
ret = sscanf( str, "0x%x", &a );
} else {
ret = sscanf( str, "%u %u %u %u", &a, &r, &g, &b );
}
if( ret == 1 ) {
return a;
} else if( ret == 2 ) {
return 0xff000000 | ( (a & 0xff) << 8 ) | (r & 0xff);
} else if( ret == 3 ) {
return 0xff000000 | ( (a & 0xff) << 16 ) | ( (r & 0xff) << 8 ) | ( g & 0xff);
} else if( ret == 4 ) {
return ( (a & 0xff) << 24 ) | ( (r & 0xff) << 16 ) | ( ( g & 0xff) << 8 ) | (b & 0xff);
}
return 0;
}
代码使用了未初始化的变量,导致不可预知或意外的结果。
假阳性?