Skip to content

Commit

Permalink
add more range checks
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Wenocur <[email protected]>
  • Loading branch information
awenocur committed Nov 29, 2017
1 parent 3260e79 commit 15c04a1
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions twobit.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,19 @@ char byte_to_base(unsigned char byte, int offset) {
char * twobit_sequence(TwoBit * ptr, const char * name, int start, int end) {
struct twobit_index * seq = find_sequence(ptr->index, name);
int size, rsize;
char * result;
char * result, * seq_dest;
int i;

if (!seq)
return NULL;

if (start > end)
return NULL;

size = seq->size;
rsize = end - start + 1;
result = (char*) malloc((rsize + 1) * sizeof(char));
seq_dest = result = (char*) malloc((rsize + 1) * sizeof(char));
if(result == NULL) return NULL;
memset(result, 'N', rsize * sizeof(char)); /* initialize */
result[rsize] = '\0';

Expand All @@ -247,6 +251,19 @@ char * twobit_sequence(TwoBit * ptr, const char * name, int start, int end) {
rsize = end - start + 1;
}

if (start < 0)
{
rsize += start;
seq_dest -= start;
start = 0;
}
if (end < 0)
{
rsize = 0;
seq_dest = result;
end = 0;
};

/* fill sequence */
{
const unsigned char * block;
Expand All @@ -259,7 +276,7 @@ char * twobit_sequence(TwoBit * ptr, const char * name, int start, int end) {

i = 0;
while (i < rsize) {
result[i] = byte_to_base(*block, offset);
seq_dest[i] = byte_to_base(*block, offset);

++i;
++offset;
Expand All @@ -286,7 +303,7 @@ char * twobit_sequence(TwoBit * ptr, const char * name, int start, int end) {
}

for (j = 0, k = bstart; j < bsize && k <= end; ++j, ++k)
result[k - start] = 'N';
seq_dest[k - start] = 'N';
}
}

Expand Down

0 comments on commit 15c04a1

Please sign in to comment.