Universal validation function for DATE_LOG, DATE_RANGE, and HOLIDAYS handles with caching and error detection
IsValidDateLogHandle is the primary validation function for date collection handles. It validates DATE_LOG_HANDLE, DATE_RANGE_HANDLE, and HOLIDAYS_HANDLE instances by checking structural integrity, magic numbers, resource availability, and handle state. The function implements a multi-level validation strategy with result caching for performance optimization.
This function serves as the gatekeeper for all handle operations:
BOOL _Check_return_ IsValidDateLogHandle(
_In_ DATE_COLLECTION_HANDLE hDateLogHandle
);
Standard C calling convention. The _Check_return_ SAL annotation marks this as a function whose return value must be checked—ignoring the return value generates compiler warnings.
| Parameter | Type | Direction | Description |
|---|---|---|---|
hDateLogHandle |
DATE_COLLECTION_HANDLE |
In | Handle to validate (DATE_LOG, DATE_RANGE, or HOLIDAYS) |
[In] DATE_COLLECTION_HANDLE
Opaque handle to a date collection (DATE_LOG, DATE_RANGE, or HOLIDAYS).
InitializeDateLogHandle()Handle is valid and can be used safely in handle operations.
Returned in any of these cases:
hDateLogHandle is NULL__IS_INVALID_MAGIC__)The function implements a hierarchical validation approach:
hDateLogHandle != NULLif (hDateLogHandle == NULL) return FALSE;
__IS_INVALID_MAGIC__if (pDateLog->dwCheckedMagic == __IS_INVALID_MAGIC__) return FALSE;
if (pDateLog->dwCheckedMagic == __CHECKED_MAGIC__) return TRUE;
if (pDateLog->pContext == NULL) return FALSE;
switch (pDateLog->pContext->dwMagic) { ... }
After dispatching by magic number, the function delegates to:
| Magic Number | Handle Type | Validator Function | Validations Performed |
|---|---|---|---|
__DATE_LOG_MAGIC__ |
DATE_LOG_HANDLE | IsValidDateLogHandleInternal() |
Core integrity, holidays linkage, max length, file info |
__RANGE_LOG_MAGIC__ |
DATE_RANGE_HANDLE | IsValidDateRangeHandle() |
Core integrity, max length, both DateLog and Range magic |
__HOLIDAY_MAGIC__ |
HOLIDAYS_HANDLE | IsValidHolidaysHandle() |
Core integrity, magic number, max holidays count |
| Any Other | Unknown/Corrupted | N/A | Returns FALSE immediately |
Each type-specific validator checks:
After successful validation, the function caches the result:
On subsequent calls, the cached __CHECKED_MAGIC__ value allows immediate return without re-validation, unless:
__IS_INVALID_MAGIC__ (cached failure)
DATE_LOG_HANDLE hDateLog = /* create handle */;
if (IsValidDateLogHandle(hDateLog)) {
// Handle is valid - safe to use
DWORD count = GetDateCollectionLength(hDateLog);
DATE_ITEM_HANDLE hFirst = GetDateLogFirstDate(hDateLog);
} else {
// Handle is invalid - cleanup and report error
printf("Error: invalid date log handle\n");
return E_INVALIDARG;
}
// Function that must validate before use
HRESULT ProcessDateLog(DATE_LOG_HANDLE hLog) {
// First check: is the handle still valid?
if (!IsValidDateLogHandle(hLog)) {
printf("Error: date log has been invalidated or corrupted\n");
return E_HANDLE;
}
// Now safe to proceed
DATE_ITEM_HANDLE hFirst = GetDateLogFirstDate(hLog);
if (hFirst == NULL) {
return E_NO_DATA;
}
// ... process dates ...
return S_OK;
}
// Validate all three handle types
BOOL ValidateAllHandles(
DATE_LOG_HANDLE hDateLog,
DATE_RANGE_HANDLE hRange,
HOLIDAYS_HANDLE hHolidays
) {
if (!IsValidDateLogHandle(hDateLog)) {
printf("Invalid date log\n");
return FALSE;
}
if (!IsValidDateLogHandle((DATE_LOG_HANDLE)hRange)) {
printf("Invalid date range\n");
return FALSE;
}
if (!IsValidDateLogHandle((DATE_LOG_HANDLE)hHolidays)) {
printf("Invalid holidays handle\n");
return FALSE;
}
return TRUE; // All handles valid
}
DATE_LOG_HANDLE hDateLog = /* create and validate once */;
// First call: full validation (all 5 checks performed)
BOOL valid1 = IsValidDateLogHandle(hDateLog);
// Second call: cached validation (only checks 1, 2, and 3 performed)
// Skips context dereferencing and type-specific validation
BOOL valid2 = IsValidDateLogHandle(hDateLog);
// Both return same result but second is faster due to caching
assert(valid1 == valid2);
IsValidDateLogHandle() is read-only and thread-safe for concurrent validation calls. Multiple threads can simultaneously validate the same handle without synchronization issues.
__CHECKED_MAGIC__ markerValidation alone does NOT provide synchronization. If the handle is used after validation:
Once marked with __IS_INVALID_MAGIC__, a handle is permanently invalid. This state cannot be cleared, ensuring failed handles are never mistakenly reused.
_Check_return_ attribute means compiler will warn if you ignore the return value. Always write:
if (!IsValidDateLogHandle(hLog)) { /* handle error */ }
InitializeDateLogHandle() - Create new handleFreeDateCollectionHandle() - Free handle (decrements reference count)IsValidDateLogHandleInternal() - Type-specific DateLog validationIsValidDateRangeHandle() - Type-specific DateRange validationIsValidHolidaysHandle() - Type-specific Holidays validationIsValidHandle() - Alternative with explicit type parameterRecommended pattern for all functions receiving handles:
HRESULT SomeFunction(DATE_LOG_HANDLE hLog) {
// Validate immediately
if (!IsValidDateLogHandle(hLog)) {
return HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE);
}
// Proceed with operation
// ...
return S_OK;
}