
PKUMAR077244 (Community Member) asked a question.
I want to know how to fix "Improper Validation of Array Index" ?
flaws statement : This array access uses an index value that does not reference a valid position in the array. The array has 2073 elements, with valid indexes ranging from 0 to 2072, and the code references an array index that is tainted (i.e. controlled by the user).Use bounds checking to ensure the validity of any user-controlled value used as an array index.References: CWE (https://cwe.mitre.org/data/definitions/129.html)
// the following un-needed checks are intended to fix SAST scan flags
if(k < SPDU_BUFFER_SIZE) sess_buff[ k++ ] = 0x21; /* Session Level Control Header */
if(k < SPDU_BUFFER_SIZE) sess_buff[ k++ ] = 0x00; /* type = session request */
if(k < SPDU_BUFFER_SIZE) sess_buff[ k++ ] = 0xC1; /* Parameters - USCI Called. */
if(k < SPDU_BUFFER_SIZE) sess_buff[ k++ ] = strlen( usci_called_addr );
if((k+strlen(usci_called_addr+1)) < SPDU_BUFFER_SIZE) strcpy( (char *) &sess_buff[k], usci_called_addr );
k += strlen( usci_called_addr );
if(k < SPDU_BUFFER_SIZE) sess_buff[ k++ ] = 0xC2; /* Parameters - USCI Calling */
if(k < SPDU_BUFFER_SIZE) sess_buff[ k++ ] = strlen( (char *) usci_calling_addr );
if((k+strlen(usci_calling_addr)+1) < SPDU_BUFFER_SIZE) strcpy( (char *) &sess_buff[k], (char *) usci_calling_addr );
k += strlen( (char *) usci_calling_addr );
.png)
Hi @PKUMAR077244 (Community Member)
As suggested, you would need to bound checking for the given untrusted index being used on array.
Verifying the index is greater than 0 but less than or equal to 2072 should be sufficient mitigation.
Regards,
Kashif.
HI kashif,
Thanks you for the help.
Thanks & Regards,
Pankaj Kumar