Commit 712e78744e3a0332825a80298f38225b30dec88c

Authored by bellard
1 parent 7c35359c

probing fixes


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1425 c046a42c-6fe2-441c-8c8c-71466251a162
block-cow.c
@@ -54,7 +54,8 @@ static int cow_probe(const uint8_t *buf, int buf_size, const char *filename) @@ -54,7 +54,8 @@ static int cow_probe(const uint8_t *buf, int buf_size, const char *filename)
54 { 54 {
55 const struct cow_header_v2 *cow_header = (const void *)buf; 55 const struct cow_header_v2 *cow_header = (const void *)buf;
56 56
57 - if (be32_to_cpu(cow_header->magic) == COW_MAGIC && 57 + if (buf_size >= sizeof(struct cow_header_v2) &&
  58 + be32_to_cpu(cow_header->magic) == COW_MAGIC &&
58 be32_to_cpu(cow_header->version) == COW_VERSION) 59 be32_to_cpu(cow_header->version) == COW_VERSION)
59 return 100; 60 return 100;
60 else 61 else
block-qcow.c
@@ -80,8 +80,9 @@ static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset); @@ -80,8 +80,9 @@ static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
80 static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) 80 static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
81 { 81 {
82 const QCowHeader *cow_header = (const void *)buf; 82 const QCowHeader *cow_header = (const void *)buf;
83 -  
84 - if (be32_to_cpu(cow_header->magic) == QCOW_MAGIC && 83 +
  84 + if (buf_size >= sizeof(QCowHeader) &&
  85 + be32_to_cpu(cow_header->magic) == QCOW_MAGIC &&
85 be32_to_cpu(cow_header->version) == QCOW_VERSION) 86 be32_to_cpu(cow_header->version) == QCOW_VERSION)
86 return 100; 87 return 100;
87 else 88 else
@@ -551,9 +552,19 @@ static int qcow_create(const char *filename, int64_t total_size, @@ -551,9 +552,19 @@ static int qcow_create(const char *filename, int64_t total_size,
551 header_size = sizeof(header); 552 header_size = sizeof(header);
552 backing_filename_len = 0; 553 backing_filename_len = 0;
553 if (backing_file) { 554 if (backing_file) {
554 - realpath(backing_file, backing_filename);  
555 - if (stat(backing_filename, &st) != 0) {  
556 - return -1; 555 + const char *p;
  556 + /* XXX: this is a hack: we do not attempt to check for URL
  557 + like syntax */
  558 + p = strchr(backing_file, ':');
  559 + if (p && (p - backing_file) >= 2) {
  560 + /* URL like but exclude "c:" like filenames */
  561 + pstrcpy(backing_filename, sizeof(backing_filename),
  562 + backing_file);
  563 + } else {
  564 + realpath(backing_file, backing_filename);
  565 + if (stat(backing_filename, &st) != 0) {
  566 + return -1;
  567 + }
557 } 568 }
558 header.mtime = cpu_to_be32(st.st_mtime); 569 header.mtime = cpu_to_be32(st.st_mtime);
559 header.backing_file_offset = cpu_to_be64(header_size); 570 header.backing_file_offset = cpu_to_be64(header_size);
block-vpc.c
@@ -81,9 +81,8 @@ typedef struct BDRVVPCState { @@ -81,9 +81,8 @@ typedef struct BDRVVPCState {
81 81
82 static int vpc_probe(const uint8_t *buf, int buf_size, const char *filename) 82 static int vpc_probe(const uint8_t *buf, int buf_size, const char *filename)
83 { 83 {
84 - if (!strncmp(buf, "conectix", 8)) 84 + if (buf_size >= 8 && !strncmp(buf, "conectix", 8))
85 return 100; 85 return 100;
86 -  
87 return 0; 86 return 0;
88 } 87 }
89 88
@@ -106,26 +106,29 @@ static BlockDriver *find_image_format(const char *filename) @@ -106,26 +106,29 @@ static BlockDriver *find_image_format(const char *filename)
106 size_t bufsize = 1024; 106 size_t bufsize = 1024;
107 107
108 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); 108 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
109 - if (fd < 0)  
110 - return NULL; 109 + if (fd < 0) {
  110 + buf = NULL;
  111 + ret = 0;
  112 + } else {
111 #ifdef DIOCGSECTORSIZE 113 #ifdef DIOCGSECTORSIZE
112 - {  
113 - unsigned int sectorsize = 512;  
114 - if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&  
115 - sectorsize > bufsize)  
116 - bufsize = sectorsize;  
117 - } 114 + {
  115 + unsigned int sectorsize = 512;
  116 + if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
  117 + sectorsize > bufsize)
  118 + bufsize = sectorsize;
  119 + }
118 #endif 120 #endif
119 - buf = malloc(bufsize);  
120 - if (!buf)  
121 - return NULL;  
122 - ret = read(fd, buf, bufsize);  
123 - if (ret < 0) { 121 + buf = qemu_malloc(bufsize);
  122 + if (!buf)
  123 + return NULL;
  124 + ret = read(fd, buf, bufsize);
  125 + if (ret < 0) {
  126 + close(fd);
  127 + qemu_free(buf);
  128 + return NULL;
  129 + }
124 close(fd); 130 close(fd);
125 - free(buf);  
126 - return NULL;  
127 } 131 }
128 - close(fd);  
129 132
130 drv = NULL; 133 drv = NULL;
131 score_max = 0; 134 score_max = 0;
@@ -136,7 +139,7 @@ static BlockDriver *find_image_format(const char *filename) @@ -136,7 +139,7 @@ static BlockDriver *find_image_format(const char *filename)
136 drv = drv1; 139 drv = drv1;
137 } 140 }
138 } 141 }
139 - free(buf); 142 + qemu_free(buf);
140 return drv; 143 return drv;
141 } 144 }
142 145
@@ -154,7 +157,7 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot, @@ -154,7 +157,7 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot,
154 bs->read_only = 0; 157 bs->read_only = 0;
155 bs->is_temporary = 0; 158 bs->is_temporary = 0;
156 bs->encrypted = 0; 159 bs->encrypted = 0;
157 - 160 +
158 if (snapshot) { 161 if (snapshot) {
159 BlockDriverState *bs1; 162 BlockDriverState *bs1;
160 int64_t total_size; 163 int64_t total_size;
@@ -183,7 +186,7 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot, @@ -183,7 +186,7 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot,
183 filename = tmp_filename; 186 filename = tmp_filename;
184 bs->is_temporary = 1; 187 bs->is_temporary = 1;
185 } 188 }
186 - 189 +
187 pstrcpy(bs->filename, sizeof(bs->filename), filename); 190 pstrcpy(bs->filename, sizeof(bs->filename), filename);
188 if (!drv) { 191 if (!drv) {
189 drv = find_image_format(filename); 192 drv = find_image_format(filename);
@@ -653,4 +656,5 @@ void bdrv_init(void) @@ -653,4 +656,5 @@ void bdrv_init(void)
653 bdrv_register(&bdrv_dmg); 656 bdrv_register(&bdrv_dmg);
654 bdrv_register(&bdrv_bochs); 657 bdrv_register(&bdrv_bochs);
655 bdrv_register(&bdrv_vpc); 658 bdrv_register(&bdrv_vpc);
  659 + bdrv_register(&bdrv_vvfat);
656 } 660 }