Skip to content

Commit 0d1c5af

Browse files
fjtrujyclaude
andcommitted
fix: [libpthreadglue] tighten error handling in OS-abstraction layer
pte_osThreadCreate previously: - ignored the return value of sceKernelCreateSema for cancelSem - on the sceKernelCreateThread failure paths, leaked the cancelSem semaphore that had just been created Reorder so each resource is validated immediately after allocation and cleaned up in reverse order on failure. Also propagate sceKernelCreateSema failures from pte_osMutexCreate and pte_osSemaphoreCreate as PTE_OS_NO_RESOURCES instead of writing a negative handle to *pHandle as if it were valid. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent b33ad13 commit 0d1c5af

1 file changed

Lines changed: 25 additions & 16 deletions

File tree

src/libpthreadglue/osal.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ pte_osResult pte_osThreadCreate(pte_osThreadEntryPoint entryPoint,
199199
int pspAttr;
200200
void *pTls;
201201
SceUID threadId;
202-
pte_osResult result;
203202
pspThreadData *pThreadData;
204203

205204
if (threadNum++ > MAX_PSP_UID) {
@@ -215,8 +214,7 @@ pte_osResult pte_osThreadCreate(pte_osThreadEntryPoint entryPoint,
215214
pTls = pteTlsThreadInit();
216215
if (pTls == NULL) {
217216
PSP_DEBUG("pteTlsThreadInit: PTE_OS_NO_RESOURCES\n");
218-
result = PTE_OS_NO_RESOURCES;
219-
goto FAIL0;
217+
return PTE_OS_NO_RESOURCES;
220218
}
221219

222220
/* Allocate some memory for our per-thread control data. We use this for:
@@ -227,10 +225,8 @@ pte_osResult pte_osThreadCreate(pte_osThreadEntryPoint entryPoint,
227225

228226
if (pThreadData == NULL) {
229227
pteTlsThreadDestroy(pTls);
230-
231228
PSP_DEBUG("malloc(pspThreadData): PTE_OS_NO_RESOURCES\n");
232-
result = PTE_OS_NO_RESOURCES;
233-
goto FAIL0;
229+
return PTE_OS_NO_RESOURCES;
234230
}
235231

236232
/* Save a pointer to our per-thread control data as a TLS value */
@@ -248,6 +244,12 @@ pte_osResult pte_osThreadCreate(pte_osThreadEntryPoint entryPoint,
248244
255, /* maximum value */
249245
0); /* options (default) */
250246

247+
if (pThreadData->cancelSem < 0) {
248+
free(pThreadData);
249+
pteTlsThreadDestroy(pTls);
250+
PSP_DEBUG("sceKernelCreateSema(cancelSem): PTE_OS_NO_RESOURCES\n");
251+
return PTE_OS_NO_RESOURCES;
252+
}
251253

252254
/* In order to emulate TLS functionality, we append the address of the TLS structure that we
253255
* allocated above to the thread's name. To set or get TLS values for this thread, the user
@@ -267,24 +269,23 @@ pte_osResult pte_osThreadCreate(pte_osThreadEntryPoint entryPoint,
267269
NULL);
268270

269271
if (threadId == (SceUID) SCE_KERNEL_ERROR_NO_MEMORY) {
272+
sceKernelDeleteSema(pThreadData->cancelSem);
270273
free(pThreadData);
271274
pteTlsThreadDestroy(pTls);
272-
273275
PSP_DEBUG("sceKernelCreateThread: PTE_OS_NO_RESOURCES\n");
274-
result = PTE_OS_NO_RESOURCES;
275-
} else if (threadId < 0) {
276+
return PTE_OS_NO_RESOURCES;
277+
}
278+
279+
if (threadId < 0) {
280+
sceKernelDeleteSema(pThreadData->cancelSem);
276281
free(pThreadData);
277282
pteTlsThreadDestroy(pTls);
278-
279283
PSP_DEBUG("sceKernelCreateThread: PTE_OS_GENERAL_FAILURE\n");
280-
result = PTE_OS_GENERAL_FAILURE;
281-
} else {
282-
*ppte_osThreadHandle = threadId;
283-
result = PTE_OS_OK;
284+
return PTE_OS_GENERAL_FAILURE;
284285
}
285286

286-
FAIL0:
287-
return result;
287+
*ppte_osThreadHandle = threadId;
288+
return PTE_OS_OK;
288289
}
289290
#endif
290291

@@ -529,6 +530,10 @@ pte_osResult pte_osMutexCreate(pte_osMutexHandle *pHandle)
529530
1, /* maximum value */
530531
0); /* options (default) */
531532

533+
if (handle < 0) {
534+
return PTE_OS_NO_RESOURCES;
535+
}
536+
532537
*pHandle = handle;
533538
return PTE_OS_OK;
534539
}
@@ -601,6 +606,10 @@ pte_osResult pte_osSemaphoreCreate(int initialValue, pte_osSemaphoreHandle *pHan
601606
SEM_VALUE_MAX, /* maximum value */
602607
0); /* options (default) */
603608

609+
if (handle < 0) {
610+
return PTE_OS_NO_RESOURCES;
611+
}
612+
604613
*pHandle = handle;
605614
return PTE_OS_OK;
606615
}

0 commit comments

Comments
 (0)