今天为了KPI,搞了一天的PPT,搞得恶心想吐。最后还是回到这儿,这儿才是我的净土,可以写写我的研究。
这儿讲一些Reactos中一些明显的错误。(转载请指明出处)
在Reactos的RegQueryInfoKeyW中有段这样的实现
if (lpcbMaxSubKeyLen != NULL)
{
*lpcbMaxSubKeyLen = FullInfo->MaxNameLen / sizeof(WCHAR) + 1;
}
if (lpcbMaxClassLen != NULL)
{
*lpcbMaxClassLen = FullInfo->MaxClassLen / sizeof(WCHAR) + 1;
}
if (lpcbMaxValueNameLen != NULL)
{
*lpcbMaxValueNameLen = FullInfo->MaxValueNameLen / sizeof(WCHAR) + 1;
}
这儿存在一个明显的错误,当待查询键不存在子键时,最长子键名长度肯定是0啊,怎么会是1呢?同样的错误出现在最长Class长度和最长项名长度。Reactos源码中对这些长度数据的处理最后都加上了1,使我很不解,看MSDN,对这些变量的说明是pcMaxSubKeyLen [out, optional]
A pointer to a variable that receives the size of the key's subkey with the longest name,in Unicode characters, not including the terminating null character. This parameter can be NULL.
lpcMaxClassLen [out, optional]
A pointer to a variable that receives the size of the longest string that specifies a subkey class, in Unicode characters. The count returned does not include the terminating null character.This parameter can be NULL.
lpcMaxValueNameLen [out, optional]
A pointer to a variable that receives the size of the key's longest value name, in Unicode characters. The size does not include the terminating null character. This parameter can be NULL.
可以见得,这些数据都不含结尾符的,那干嘛还要+1呢?我只能理解为,如果数据没按WCHAR对齐,在除以sizeof(WCHAR)后,数据将“缺”一位,于是就要多加一个1。但是它没有考虑到子键和子项不存在的情况。
我修改后的代码是
if ( NULL != lpcMaxSubKeyLen ) {
*lpcMaxSubKeyLen = ( ( FullInfo->MaxNameLen + 1 ) & ~1 ) / sizeof(WCHAR);
}
if ( NULL != lpcMaxClassLen ) {
*lpcMaxClassLen = ( ( FullInfo->MaxClassLen + 1 ) & ~1 ) / sizeof(WCHAR);
}
if ( NULL != lpcMaxValueNameLen ) {
*lpcMaxValueNameLen = ( ( FullInfo->MaxValueNameLen + 1 ) & ~1 ) / sizeof(WCHAR);
}