界: Code Quality

程式碼品質不佳,會導致無法預料的行為。從使用者的角度來看,這通常表現為可用性不佳。對於攻擊者而言,這提供了以意想不到的方式向系統施加壓力的機會。

Code Correctness: Premature Thread Termination

Abstract
如果主處理在其衍生的執行緒之前先正確完成執行,就會提早終止該執行緒。
Explanation
若主處理在其衍生的執行緒之前先完成執行且沒有呼叫 pthread_exit(),就會先終止由主處理 main() 函數的 pthread_create() 所呼叫而洐生的執行緒。呼叫 pthread_exit() 保證主處理會持續進行,直到所有執行緒完成執運作方式止。或者,主處理可呼叫所有子執行緒上的 pthread_join,並確定這些執行緒將在處理結束前完成。

範例 1:以下程式碼使用 pthread_create() 來建立執行緒,然後正常結束。如果子執行緒在 main() 函數回傳時沒有完整執行,就會先被終止。


void *Simple(void *threadid)
{
...
pthread_exit(NULL);
}

int main(int argc, char *argv[]) {
int rc;
pthread_t pt;
rc = pthread_create(&pt, NULL, Simple, (void *)t);
if (rc){
exit(-1);
}
}
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Motor Industry Software Reliability Association (MISRA) C Guidelines 2012 Rule 1.3
desc.controlflow.cpp.code_correctness_premature_thread_termination