开发者

Any reason I shoudn't add GUID validation to my Visual C++ program?

开发者 https://www.devze.com 2023-02-01 02:05 出处:网络
In order to make my Visual C++ program more robust I\'m tempted to insert checks that GUID variables contain valid version 4 GUIDs (and are not left uninitialized):

In order to make my Visual C++ program more robust I'm tempted to insert checks that GUID variables contain valid version 4 GUIDs (and are not left uninitialized):

GUID guid;
UuidCreate( &guid );
// many lines of code later...
// the following assert should not fire for valid version 4 GUIDs
int data3 = guid.Data3;
assert( ( data3 >> 12 ) == 4 );

I'm completely sure that all GUIDs will either normally come from within UuidCreate() function or be uninitialized variables (and the latter is what I'd like to diagnose with those checks). My only concern is Microsoft could suddenly change GUID implementation in future versions of Windows.

What other factors should I consider to decide whether such checks won't hurt? Also how likely is it that GUID implementation changes on f开发者_StackOverflowuture versions of Windows?


My only concern is Microsoft could suddenly change GUID implementation in future versions of Windows.

Deal with it, if it happens. No, seriously, you could also worry about the implementation of integer, Microsoft could decide to switch to the .NET datatypes and make the integer system-wide a 4-byte value.

Worry about other things, if implementations are changing, you'll have to rewrite/change/fix your program anyway.

Edit: If I understand you right, you're worrying about the return value of the function, not the GUID-Structure itself. And you mean that at the moment it can return either a GUID or the uninitialized value, but in future implementations it might return a 0-filled-GUID?

In that case I stick to my first paragraph, worry about it if it happens. At the moment the function has a clearly defined behavior, work with that. Check for uninitialized values and that's it. You can't guess what Microsoft will do in the future (hell, sometimes even Microsoft can't).


Well, let's see... you're calling UuidCreate without being able to specify a particular GUID version, and you're going to expect a particular version in return? How is that a good idea?

If you're trying to make your software "more robust", how about initializing your variables and checking for errors?

GUID guid = {0};
if (SUCCEEDED(UuidCreate( &guid ) ) {
    // you can be _pretty_ sure guid has some valid stuff in it
}
0

精彩评论

暂无评论...
验证码 换一张
取 消