Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed overflow in state_count #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ uint16_t parse_waveform(char* data, uint32_t* wav_addrs, uint32_t wav_addr, FILE
i += 2;
}

state_count += count * 4;
state_count += count;

if(outfile) {

Expand Down Expand Up @@ -627,7 +627,7 @@ int parse_temp_ranges(struct waveform_data_header* header, char* data, char* tr_
}

if(do_print) {
printf("%4u phases\n", state_count / 256);
printf("%4u phases\n", state_count >> 6);
}

if(outfile) {
Expand All @@ -641,13 +641,20 @@ int parse_temp_ranges(struct waveform_data_header* header, char* data, char* tr_
return -1;
}

state_count = htons(state_count);
// write state count
written = fwrite(&state_count, sizeof(state_count), 1, outfile);
if(written != 1) {
fprintf(stderr, "Error writing state count to output file: %s\n", strerror(errno));
if(state_count > 0x3FFF){
fprintf(stderr, "state_count when shifted would occupy 4 bytes: %d\n", state_count);
return -1;
}
{
uint16_t state_count_uint16_t = state_count << 2;
state_count_uint16_t = htons(state_count_uint16_t);
// write state count
written = fwrite(&state_count_uint16_t, sizeof(state_count_uint16_t), 1, outfile);
if(written != 1) {
fprintf(stderr, "Error writing state count to output file: %s\n", strerror(errno));
return -1;
}
}

// restore file position to end of previously written data
if(fseek(outfile, fcur, SEEK_SET) < 0) {
Expand Down