Skip to content
This repository has been archived by the owner on Nov 11, 2024. It is now read-only.

Commit

Permalink
POSSIBLY BREAKING CHANGE for Linux uPortUartPrefix(): range check. (#…
Browse files Browse the repository at this point in the history
…1113)

Should a prefix be passed to uPortUartPrefix() that was greater than 32 characters in length, it would be accepted and truncated to 32 characters but, potentially, no terminator might be added; the truncation is done with strncpy() but strncpy() doesn't _guarantee_ a terminator, it just guarantees no overrun.  The length is now checked on entry so that a prefix that is too long is rejected; hence a terminator is now guaranteed.  Also the names of a few static functions are updated to follow the pointer syntax used elsewhere.

THE POSSIBLY BREAKING PART is that you may have previously been setting a prefix that was too long and getting away with it: you will now get back U_ERROR_COMMON_INVALID_PARAMETER instead.
  • Loading branch information
RobMeades authored Mar 9, 2024
1 parent 3806278 commit 208ef1b
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions port/platform/linux/src/u_port_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ static void readTask(void *pParam)
}
}

static uPortUartPrefix_t *findPrefix(pthread_t threadId)
static uPortUartPrefix_t *pFindPrefix(pthread_t threadId)
{
uLinkedList_t *p = gpUartPrefixList;
while (p != NULL) {
Expand All @@ -228,7 +228,7 @@ static uPortUartPrefix_t *findPrefix(pthread_t threadId)
return NULL;
}

static uPortUartData_t *findUart(int32_t handle)
static uPortUartData_t *pFindUart(int32_t handle)
{
uLinkedList_t *p = gpUartList;
while (p != NULL) {
Expand All @@ -241,7 +241,7 @@ static uPortUartData_t *findUart(int32_t handle)
return NULL;
}

static uPortUartData_t *findUartById(int32_t id)
static uPortUartData_t *pFindUartById(int32_t id)
{
uLinkedList_t *p = gpUartList;
while (p != NULL) {
Expand Down Expand Up @@ -288,7 +288,7 @@ static uint32_t suspendResumeUartHwHandshake(int32_t handle, bool suspendNotResu
if (gMutex != NULL) {
U_PORT_MUTEX_LOCK(gMutex);
errorCode = U_ERROR_COMMON_INVALID_PARAMETER;
uPortUartData_t *pUartData = findUart(handle);
uPortUartData_t *pUartData = pFindUart(handle);
if ((pUartData != NULL) && !pUartData->markedForDeletion) {
errorCode = U_ERROR_COMMON_SUCCESS;
struct termios options;
Expand Down Expand Up @@ -378,15 +378,15 @@ void uPortUartDeinit()
int32_t uPortUartPrefix(const char *pPrefix)
{
uErrorCode_t errorCode = U_ERROR_COMMON_INVALID_PARAMETER;
if (pPrefix != NULL) {
if ((pPrefix != NULL) && (strlen(pPrefix) <= U_PORT_UART_MAX_PREFIX_LENGTH)) {
errorCode = U_ERROR_COMMON_NO_MEMORY;
uPortUartPrefix_t *pUartPrefix = pUPortMalloc(sizeof(uPortUartPrefix_t));
if (pUartPrefix != NULL) {
U_PORT_MUTEX_LOCK(gMutex);
// Remove any existing prefixes for this thread ID
pthread_t threadId = pthread_self();
uPortUartPrefix_t *p;
while ((p = findPrefix(threadId)) != NULL) {
while ((p = pFindPrefix(threadId)) != NULL) {
uPortFree(p);
uLinkedListRemove(&gpUartPrefixList, p);
}
Expand Down Expand Up @@ -416,7 +416,7 @@ int32_t uPortUartOpen(int32_t uart, int32_t baudRate,
if (gMutex == NULL) {
return U_ERROR_COMMON_NOT_INITIALISED;
}
if (findUartById(uart) != NULL) {
if (pFindUartById(uart) != NULL) {
return (int32_t)U_ERROR_COMMON_BUSY;
}
uPortUartData_t *pUartData = pUPortMalloc(sizeof(uPortUartData_t));
Expand Down Expand Up @@ -453,7 +453,7 @@ int32_t uPortUartOpen(int32_t uart, int32_t baudRate,
char prefix[U_PORT_UART_MAX_PREFIX_LENGTH + 1]; // +1 for terminator
char portName[U_PORT_UART_MAX_PREFIX_LENGTH + 16]; // +16 for terminator and uart index
U_PORT_MUTEX_LOCK(gMutex);
uPortUartPrefix_t *pUartPrefix = findPrefix(pthread_self());
uPortUartPrefix_t *pUartPrefix = pFindPrefix(pthread_self());
if (pUartPrefix != NULL) {
strncpy(prefix, pUartPrefix->str, sizeof(prefix));
} else {
Expand Down Expand Up @@ -525,7 +525,7 @@ void uPortUartClose(int32_t handle)
{
if (gMutex != NULL) {

uPortUartData_t *pUartData = findUart(handle);
uPortUartData_t *pUartData = pFindUart(handle);

U_PORT_MUTEX_LOCK(gMutex);

Expand All @@ -550,7 +550,7 @@ int32_t uPortUartGetReceiveSize(int32_t handle)
if (gMutex != NULL) {
sizeOrErrorCode = (int32_t)U_ERROR_COMMON_INVALID_PARAMETER;
U_PORT_MUTEX_LOCK(gMutex);
uPortUartData_t *pUartData = findUart(handle);
uPortUartData_t *pUartData = pFindUart(handle);
if ((pUartData != NULL) && !pUartData->markedForDeletion) {
if (pUartData->bufferFull) {
sizeOrErrorCode = pUartData->bufferSize;
Expand Down Expand Up @@ -578,7 +578,7 @@ int32_t uPortUartRead(int32_t handle, void *pBuffer,
if (gMutex != NULL) {
U_PORT_MUTEX_LOCK(gMutex);
sizeOrErrorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
uPortUartData_t *pUartData = findUart(handle);
uPortUartData_t *pUartData = pFindUart(handle);
if ((pBuffer != NULL) && (sizeBytes > 0) &&
(pUartData != NULL) && !pUartData->markedForDeletion) {
sizeOrErrorCode = 0;
Expand Down Expand Up @@ -629,7 +629,7 @@ int32_t uPortUartWrite(int32_t handle, const void *pBuffer,
if (gMutex != NULL) {
U_PORT_MUTEX_LOCK(gMutex);
sizeOrErrorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
uPortUartData_t *pUartData = findUart(handle);
uPortUartData_t *pUartData = pFindUart(handle);
if ((pBuffer != NULL) && (sizeBytes > 0) &&
(pUartData != NULL) && !pUartData->markedForDeletion) {
sizeOrErrorCode = write(pUartData->uartFd, pBuffer, sizeBytes);
Expand All @@ -656,7 +656,7 @@ int32_t uPortUartEventCallbackSet(int32_t handle,
if (gMutex != NULL) {
U_PORT_MUTEX_LOCK(gMutex);
errorCode = U_ERROR_COMMON_INVALID_PARAMETER;
uPortUartData_t *pUartData = findUart(handle);
uPortUartData_t *pUartData = pFindUart(handle);
if ((pUartData != NULL) && !pUartData->markedForDeletion &&
(filter != 0) && (pFunction != NULL)) {
// Open an event queue to eventHandler()
Expand Down Expand Up @@ -690,7 +690,7 @@ void uPortUartEventCallbackRemove(int32_t handle)
int32_t eventQueueHandle = -1;
if (gMutex != NULL) {
U_PORT_MUTEX_LOCK(gMutex);
uPortUartData_t *pUartData = findUart(handle);
uPortUartData_t *pUartData = pFindUart(handle);
if ((pUartData != NULL) && !pUartData->markedForDeletion) {
// Save the eventQueueHandle and set all
// the parameters to indicate that the
Expand Down Expand Up @@ -718,7 +718,7 @@ uint32_t uPortUartEventCallbackFilterGet(int32_t handle)
uint32_t filter = 0;
if (gMutex != NULL) {
U_PORT_MUTEX_LOCK(gMutex);
uPortUartData_t *pUartData = findUart(handle);
uPortUartData_t *pUartData = pFindUart(handle);
if ((pUartData != NULL) && !pUartData->markedForDeletion) {
filter = pUartData->eventFilter;
}
Expand All @@ -734,7 +734,7 @@ int32_t uPortUartEventCallbackFilterSet(int32_t handle,
uErrorCode_t errorCode = U_ERROR_COMMON_NOT_INITIALISED;
if (gMutex != NULL) {
U_PORT_MUTEX_LOCK(gMutex);
uPortUartData_t *pUartData = findUart(handle);
uPortUartData_t *pUartData = pFindUart(handle);
if ((filter != 0) && (pUartData != NULL) &&
!pUartData->markedForDeletion) {
pUartData->eventFilter = filter;
Expand All @@ -752,7 +752,7 @@ int32_t uPortUartEventSend(int32_t handle, uint32_t eventBitMap)
if (gMutex != NULL) {
U_PORT_MUTEX_LOCK(gMutex);
errorCode = U_ERROR_COMMON_INVALID_PARAMETER;
uPortUartData_t *pUartData = findUart(handle);
uPortUartData_t *pUartData = pFindUart(handle);
if ((pUartData != NULL) && !pUartData->markedForDeletion &&
(pUartData->eventQueueHandle >= 0) &&
// The only event we support right now
Expand Down Expand Up @@ -788,7 +788,7 @@ bool uPortUartEventIsCallback(int32_t handle)
bool isEventCallback = false;
if (gMutex != NULL) {
U_PORT_MUTEX_LOCK(gMutex);
uPortUartData_t *pUartData = findUart(handle);
uPortUartData_t *pUartData = pFindUart(handle);
if ((pUartData != NULL) && !pUartData->markedForDeletion &&
(pUartData->eventQueueHandle >= 0)) {
isEventCallback = uPortEventQueueIsTask(pUartData->eventQueueHandle);
Expand All @@ -805,7 +805,7 @@ int32_t uPortUartEventStackMinFree(int32_t handle)
if (gMutex != NULL) {
U_PORT_MUTEX_LOCK(gMutex);
sizeOrErrorCode = (int32_t) U_ERROR_COMMON_INVALID_PARAMETER;
uPortUartData_t *pUartData = findUart(handle);
uPortUartData_t *pUartData = pFindUart(handle);
if ((pUartData != NULL) && !pUartData->markedForDeletion &&
(pUartData->eventQueueHandle >= 0)) {
sizeOrErrorCode = uPortEventQueueStackMinFree(pUartData->eventQueueHandle);
Expand All @@ -822,7 +822,7 @@ bool uPortUartIsRtsFlowControlEnabled(int32_t handle)
bool rtsFlowControlIsEnabled = false;
if (gMutex != NULL) {
U_PORT_MUTEX_LOCK(gMutex);
uPortUartData_t *pUartData = findUart(handle);
uPortUartData_t *pUartData = pFindUart(handle);
if ((pUartData != NULL) && !pUartData->markedForDeletion) {
rtsFlowControlIsEnabled = pUartData->hwHandshake || pUartData->handshakeSuspended;
}
Expand Down

0 comments on commit 208ef1b

Please sign in to comment.