Skip to content

Fix out of bounds memory access in radix sort #16

Description

@yarnellr

The radix sort currently has an out of bounds memory access issue in the histogram function. This causes an issue in hardware due to the access to address BUCKETSIZE causing an access to address 0 due to the bucket address having log(BUCKETSIZE) bits. This causes memory corruption and design failure.

Propose changing the following code:

void hist(int bucket[BUCKETSIZE], int a[SIZE], int exp)
{
int blockID, i, bucket_indx, a_indx;
blockID = 0;
hist_1 : for (blockID=0; blockID<NUMOFBLOCKS; blockID++) {
hist_2 : for(i=0; i<4; i++) {
a_indx = blockID * ELEMENTSPERBLOCK + i;
bucket_indx = ((a[a_indx] >> exp) & 0x3)*NUMOFBLOCKS + blockID + 1;
bucket[bucket_indx]++;
}
}
}

To this:

void hist(int bucket[BUCKETSIZE], int a[SIZE], int exp)
{
int blockID, i, bucket_indx, a_indx;
blockID = 0;
hist_1 : for (blockID=0; blockID<NUMOFBLOCKS; blockID++) {
hist_2 : for(i=0; i<4; i++) {
a_indx = blockID * ELEMENTSPERBLOCK + i;
bucket_indx = ((a[a_indx] >> exp) & 0x3)*NUMOFBLOCKS + blockID + 1;
if (bucket_indx < BUCKETSIZE) {
bucket[bucket_indx]++;
}
}
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions