dos33_raw: add some sanity checks

try to warn a bit if we write beyond the end of what we expect
This commit is contained in:
Vince Weaver 2021-04-26 15:24:48 -04:00
parent a61feb848f
commit 6475967948
1 changed files with 36 additions and 3 deletions

View File

@ -5,6 +5,8 @@
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
/* usage: dos33_raw track sector file */
static void usage(char *exe_name) {
printf("Usage:\n");
@ -31,13 +33,15 @@ static int goto_dos_track_sector(int fd, int track, int sector) {
int main(int argc, char **argv) {
unsigned int track,sector,start,count,total;
unsigned int track,sector,start,count,total,max_track,filesize;
unsigned int max_sector,check_max=0;
int disk_image_fd;
int file_fd;
unsigned char buffer[256];
int result,read_result;
struct stat statbuf;
if (argc<6) {
if (argc<7) {
usage(argv[0]);
}
@ -46,7 +50,34 @@ int main(int argc, char **argv) {
start=atoi(argv[5]);
count=atoi(argv[6]);
/* FIXME: check limits based on stat of file */
if (argc>7) {
max_track=atoi(argv[7]);
check_max=1;
}
/* check filesize using stat */
result=stat(argv[4], &statbuf);
if (result<0) {
fprintf(stderr,"Error stating %s: %s\n",
argv[4],strerror(errno));
exit(1);
}
filesize=statbuf.st_size;
if (count==0) {
count=(filesize/256);
if ((filesize%256)!=0) count++;
}
/* sanity check we aren't going off the last track */
if (check_max) {
max_sector=((track*16)+sector+count);
if (max_sector >= max_track*16) {
fprintf(stderr,"Error, %d exceeds max_sector of %d\n",
max_sector,max_track*16);
exit(1);
}
}
if (track>34) {
fprintf(stderr,"Warning! Unusual track number %d\n",track);
@ -109,5 +140,7 @@ int main(int argc, char **argv) {
close(file_fd);
close(disk_image_fd);
fprintf(stderr,"Wrote %d sectors\n",count);
return 0;
}