Skip to content

Commit

Permalink
Fix CodeQL warnings, part 2 (#3585)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanTLavavej authored Mar 24, 2023
1 parent c353b68 commit f0bf77e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 21 deletions.
4 changes: 2 additions & 2 deletions stl/inc/xbit_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ _NODISCARD inline unsigned long _Floor_of_log_2(size_t _Value) noexcept { // ret

#else // ^^^ _M_CEE_PURE / !_M_CEE_PURE vvv
#ifdef _WIN64
_BitScanReverse64(&_Result, _Value);
_BitScanReverse64(&_Result, _Value); // lgtm [cpp/conditionallyuninitializedvariable]
#else // ^^^ 64-bit / 32-bit vvv
_BitScanReverse(&_Result, _Value);
_BitScanReverse(&_Result, _Value); // lgtm [cpp/conditionallyuninitializedvariable]
#endif // 64 vs. 32-bit
#endif // _M_CEE_PURE

Expand Down
7 changes: 5 additions & 2 deletions stl/inc/xtimec.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ struct xtime { // store time with nanosecond resolution
long nsec;
};

_CRTIMP2_PURE int __cdecl xtime_get(xtime*, int);

_CRTIMP2_PURE long __cdecl _Xtime_diff_to_millis2(const xtime*, const xtime*);
_CRTIMP2_PURE long long __cdecl _Xtime_get_ticks();

#ifdef _CRTBLD
// Used by several src files, but not dllexported.
void _Xtime_get2(xtime*);
#endif // _CRTBLD

_CRTIMP2_PURE long long __cdecl _Query_perf_counter();
_CRTIMP2_PURE long long __cdecl _Query_perf_frequency();

Expand Down
4 changes: 2 additions & 2 deletions stl/src/cond.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ int _Cnd_timedwait(const _Cnd_t cond, const _Mtx_t mtx, const xtime* const targe
_Mtx_reset_owner(mtx);
} else { // target time specified, wait for it
xtime now;
xtime_get(&now, TIME_UTC);
_Xtime_get2(&now);
_Mtx_clear_owner(mtx);
if (!cond->_get_cv()->wait_for(cs, _Xtime_diff_to_millis2(target, &now))) { // report timeout
xtime_get(&now, TIME_UTC);
_Xtime_get2(&now);
if (_Xtime_diff_to_millis2(target, &now) == 0) {
res = _Thrd_timedout;
}
Expand Down
4 changes: 2 additions & 2 deletions stl/src/cthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ int _Thrd_detach(_Thrd_t thr) { // tell OS to release thread's resources when it

void _Thrd_sleep(const xtime* xt) { // suspend thread until time xt
xtime now;
xtime_get(&now, TIME_UTC);
_Xtime_get2(&now);
do { // sleep and check time
Sleep(_Xtime_diff_to_millis2(xt, &now));
xtime_get(&now, TIME_UTC);
_Xtime_get2(&now);
} while (now.sec < xt->sec || now.sec == xt->sec && now.nsec < xt->nsec);
}

Expand Down
4 changes: 2 additions & 2 deletions stl/src/mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static int mtx_do_lock(_Mtx_t mtx, const xtime* target) { // lock mutex

} else { // check timeout
xtime now;
xtime_get(&now, TIME_UTC);
_Xtime_get2(&now);
while (now.sec < target->sec || now.sec == target->sec && now.nsec < target->nsec) { // time has not expired
if (mtx->thread_id == static_cast<long>(GetCurrentThreadId())
|| mtx->_get_cs()->try_lock_for(_Xtime_diff_to_millis2(target, &now))) { // stop waiting
Expand All @@ -126,7 +126,7 @@ static int mtx_do_lock(_Mtx_t mtx, const xtime* target) { // lock mutex
res = WAIT_TIMEOUT;
}

xtime_get(&now, TIME_UTC);
_Xtime_get2(&now);
}
}

Expand Down
26 changes: 18 additions & 8 deletions stl/src/vector_algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,10 @@ namespace {
// Select the smallest vertical indices from the smallest element mask
_Mask &= _mm_movemask_epi8(_Traits::_Cmp_eq(_Idx_min, _Idx_min_val));
unsigned long _H_pos;
_BitScanForward(&_H_pos, _Mask); // Find the smallest horizontal index

// Find the smallest horizontal index
_BitScanForward(&_H_pos, _Mask); // lgtm [cpp/conditionallyuninitializedvariable]

const auto _V_pos = _Traits::_Get_v_pos(_Cur_idx_min, _H_pos); // Extract its vertical index
_Res._Min = _Base + _V_pos * 16 + _H_pos; // Finally, compute the pointer
}
Expand Down Expand Up @@ -908,7 +911,10 @@ namespace {
const __m128i _Idx_max = _Traits::_H_max_u(_Idx_max_val); // The greatest indices
// Select the greatest vertical indices from the largest element mask
_Mask &= _mm_movemask_epi8(_Traits::_Cmp_eq(_Idx_max, _Idx_max_val));
_BitScanReverse(&_H_pos, _Mask); // Find the largest horizontal index

// Find the largest horizontal index
_BitScanReverse(&_H_pos, _Mask); // lgtm [cpp/conditionallyuninitializedvariable]

_H_pos -= sizeof(_Cur_max_val) - 1; // Correct from highest val bit to lowest
} else {
// Looking for the first occurrence of maximum
Expand All @@ -918,7 +924,9 @@ namespace {
const __m128i _Idx_max = _Traits::_H_min_u(_Idx_max_val); // The smallest indices
// Select the smallest vertical indices from the largest element mask
_Mask &= _mm_movemask_epi8(_Traits::_Cmp_eq(_Idx_max, _Idx_max_val));
_BitScanForward(&_H_pos, _Mask); // Find the smallest horizontal index

// Find the smallest horizontal index
_BitScanForward(&_H_pos, _Mask); // lgtm [cpp/conditionallyuninitializedvariable]
}

const auto _V_pos = _Traits::_Get_v_pos(_Cur_idx_max, _H_pos); // Extract its vertical index
Expand Down Expand Up @@ -1206,7 +1214,8 @@ namespace {
__m256i _Data = _mm256_load_si256(static_cast<const __m256i*>(_First));
unsigned int _Bingo = static_cast<unsigned int>(_mm256_movemask_epi8(_Traits::_Cmp_avx(_Data, _Comparand)));

if ((_Bingo &= _Mask) != 0) {
_Bingo &= _Mask;
if (_Bingo != 0) {
unsigned long _Offset = _tzcnt_u32(_Bingo);
_Advance_bytes(_First, _Offset);
return _First;
Expand Down Expand Up @@ -1241,9 +1250,10 @@ namespace {
__m128i _Data = _mm_load_si128(static_cast<const __m128i*>(_First));
unsigned int _Bingo = static_cast<unsigned int>(_mm_movemask_epi8(_Traits::_Cmp_sse(_Data, _Comparand)));

if ((_Bingo &= _Mask) != 0) {
_Bingo &= _Mask;
if (_Bingo != 0) {
unsigned long _Offset;
_BitScanForward(&_Offset, _Bingo);
_BitScanForward(&_Offset, _Bingo); // lgtm [cpp/conditionallyuninitializedvariable]
_Advance_bytes(_First, _Offset);
return _First;
}
Expand All @@ -1254,7 +1264,7 @@ namespace {

if (_Bingo != 0) {
unsigned long _Offset;
_BitScanForward(&_Offset, _Bingo);
_BitScanForward(&_Offset, _Bingo); // lgtm [cpp/conditionallyuninitializedvariable]
_Advance_bytes(_First, _Offset);
return _First;
}
Expand Down Expand Up @@ -1301,7 +1311,7 @@ namespace {

if (_Bingo != 0) {
unsigned long _Offset;
_BitScanForward(&_Offset, _Bingo);
_BitScanForward(&_Offset, _Bingo); // lgtm [cpp/conditionallyuninitializedvariable]
_Advance_bytes(_First, _Offset);
return _First;
}
Expand Down
8 changes: 5 additions & 3 deletions stl/src/xtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ _CRTIMP2_PURE long long __cdecl _Xtime_get_ticks() { // get system time in 100-n
return ((static_cast<long long>(ft.dwHighDateTime)) << 32) + static_cast<long long>(ft.dwLowDateTime) - _Epoch;
}

static void sys_get_time(xtime* xt) { // get system time with nanosecond resolution
// Used by several src files, but not dllexported.
void _Xtime_get2(xtime* xt) { // get system time with nanosecond resolution
unsigned long long now = _Xtime_get_ticks();
xt->sec = static_cast<__time64_t>(now / _Nsec100_per_sec);
xt->nsec = static_cast<long>(now % _Nsec100_per_sec) * 100;
Expand All @@ -67,15 +68,16 @@ _CRTIMP2_PURE long __cdecl _Xtime_diff_to_millis2(const xtime* xt1, const xtime*
// TRANSITION, ABI: preserved for binary compatibility
_CRTIMP2_PURE long __cdecl _Xtime_diff_to_millis(const xtime* xt) { // convert time to milliseconds
xtime now;
xtime_get(&now, TIME_UTC);
_Xtime_get2(&now);
return _Xtime_diff_to_millis2(xt, &now);
}

// TRANSITION, ABI: preserved for binary compatibility
_CRTIMP2_PURE int __cdecl xtime_get(xtime* xt, int type) { // get current time
if (type != TIME_UTC || xt == nullptr) {
type = 0;
} else {
sys_get_time(xt);
_Xtime_get2(xt);
}

return type;
Expand Down

0 comments on commit f0bf77e

Please sign in to comment.