Skip to content

Commit 3e44477

Browse files
committed
Fix mtar file size truncation to 4G
1 parent 670cd88 commit 3e44477

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

third_party/microtar/src/microtar.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ typedef struct {
4141
} mtar_raw_header_t;
4242

4343

44-
static unsigned round_up(unsigned n, unsigned incr) {
44+
static mtar_size_t round_up(mtar_size_t n, unsigned incr) {
4545
return n + (incr - n % incr) % incr;
4646
}
4747

@@ -60,14 +60,14 @@ static unsigned checksum(const mtar_raw_header_t* rh) {
6060
}
6161

6262

63-
static int tread(mtar_t *tar, void *data, unsigned size) {
63+
static int tread(mtar_t *tar, void *data, mtar_size_t size) {
6464
int err = tar->read(tar, data, size);
6565
tar->pos += size;
6666
return err;
6767
}
6868

6969

70-
static int twrite(mtar_t *tar, const void *data, unsigned size) {
70+
static int twrite(mtar_t *tar, const void *data, mtar_size_t size) {
7171
int err = tar->write(tar, data, size);
7272
tar->pos += size;
7373
return err;
@@ -105,7 +105,7 @@ static int raw_to_header(mtar_header_t *h, const mtar_raw_header_t *rh) {
105105
/* Load raw header into header */
106106
sscanf(rh->mode, "%o", &h->mode);
107107
sscanf(rh->owner, "%o", &h->owner);
108-
sscanf(rh->size, "%o", &h->size);
108+
sscanf(rh->size, "%lo", &h->size);
109109
sscanf(rh->mtime, "%o", &h->mtime);
110110
h->type = rh->type;
111111
strcpy(h->name, rh->name);
@@ -122,7 +122,7 @@ static int header_to_raw(mtar_raw_header_t *rh, const mtar_header_t *h) {
122122
memset(rh, 0, sizeof(*rh));
123123
sprintf(rh->mode, "%o", h->mode);
124124
sprintf(rh->owner, "%o", h->owner);
125-
sprintf(rh->size, "%o", h->size);
125+
sprintf(rh->size, "%lo", h->size);
126126
sprintf(rh->mtime, "%o", h->mtime);
127127
rh->type = h->type ? h->type : MTAR_TREG;
128128
strcpy(rh->name, h->name);
@@ -153,17 +153,17 @@ const char* mtar_strerror(int err) {
153153
}
154154

155155

156-
static int file_write(mtar_t *tar, const void *data, unsigned size) {
157-
unsigned res = fwrite(data, 1, size, tar->stream);
156+
static int file_write(mtar_t *tar, const void *data, mtar_size_t size) {
157+
mtar_size_t res = fwrite(data, 1, size, tar->stream);
158158
return (res == size) ? MTAR_ESUCCESS : MTAR_EWRITEFAIL;
159159
}
160160

161-
static int file_read(mtar_t *tar, void *data, unsigned size) {
162-
unsigned res = fread(data, 1, size, tar->stream);
161+
static int file_read(mtar_t *tar, void *data, mtar_size_t size) {
162+
mtar_size_t res = fread(data, 1, size, tar->stream);
163163
return (res == size) ? MTAR_ESUCCESS : MTAR_EREADFAIL;
164164
}
165165

166-
static int file_seek(mtar_t *tar, unsigned offset) {
166+
static int file_seek(mtar_t *tar, mtar_size_t offset) {
167167
int res = fseek(tar->stream, offset, SEEK_SET);
168168
return (res == 0) ? MTAR_ESUCCESS : MTAR_ESEEKFAIL;
169169
}
@@ -213,7 +213,7 @@ int mtar_close(mtar_t *tar) {
213213
}
214214

215215

216-
int mtar_seek(mtar_t *tar, unsigned pos) {
216+
int mtar_seek(mtar_t *tar, mtar_size_t pos) {
217217
int err = tar->seek(tar, pos);
218218
tar->pos = pos;
219219
return err;
@@ -228,7 +228,8 @@ int mtar_rewind(mtar_t *tar) {
228228

229229

230230
int mtar_next(mtar_t *tar) {
231-
int err, n;
231+
mtar_size_t n;
232+
int err;
232233
mtar_header_t h;
233234
/* Load header */
234235
err = mtar_read_header(tar, &h);
@@ -287,7 +288,7 @@ int mtar_read_header(mtar_t *tar, mtar_header_t *h) {
287288
}
288289

289290

290-
int mtar_read_data(mtar_t *tar, void *ptr, unsigned size) {
291+
int mtar_read_data(mtar_t *tar, void *ptr, mtar_size_t size) {
291292
int err;
292293
/* If we have no remaining data then this is the first read, we get the size,
293294
* set the remaining data and seek to the beginning of the data */
@@ -329,7 +330,7 @@ int mtar_write_header(mtar_t *tar, const mtar_header_t *h) {
329330
}
330331

331332

332-
int mtar_write_file_header(mtar_t *tar, const char *name, unsigned size) {
333+
int mtar_write_file_header(mtar_t *tar, const char *name, mtar_size_t size) {
333334
mtar_header_t h;
334335
/* Build header */
335336
memset(&h, 0, sizeof(h));
@@ -354,7 +355,7 @@ int mtar_write_dir_header(mtar_t *tar, const char *name) {
354355
}
355356

356357

357-
int mtar_write_data(mtar_t *tar, const void *data, unsigned size) {
358+
int mtar_write_data(mtar_t *tar, const void *data, mtar_size_t size) {
358359
int err;
359360
/* Write data */
360361
err = twrite(tar, data, size);

third_party/microtar/src/microtar.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
#include <stdio.h>
1212
#include <stdlib.h>
13+
#include <stdint.h>
1314

1415
#define MTAR_VERSION "0.1.0"
1516

17+
typedef size_t mtar_size_t;
18+
1619
enum {
1720
MTAR_ESUCCESS = 0,
1821
MTAR_EFAILURE = -1,
@@ -38,7 +41,7 @@ enum {
3841
typedef struct {
3942
unsigned mode;
4043
unsigned owner;
41-
unsigned size;
44+
mtar_size_t size;
4245
unsigned mtime;
4346
unsigned type;
4447
char name[100];
@@ -49,14 +52,14 @@ typedef struct {
4952
typedef struct mtar_t mtar_t;
5053

5154
struct mtar_t {
52-
int (*read)(mtar_t *tar, void *data, unsigned size);
53-
int (*write)(mtar_t *tar, const void *data, unsigned size);
54-
int (*seek)(mtar_t *tar, unsigned pos);
55+
int (*read)(mtar_t *tar, void *data, mtar_size_t size);
56+
int (*write)(mtar_t *tar, const void *data, mtar_size_t size);
57+
int (*seek)(mtar_t *tar, mtar_size_t pos);
5558
int (*close)(mtar_t *tar);
5659
void *stream;
57-
unsigned pos;
58-
unsigned remaining_data;
59-
unsigned last_header;
60+
mtar_size_t pos;
61+
mtar_size_t remaining_data;
62+
mtar_size_t last_header;
6063
};
6164

6265

@@ -65,17 +68,17 @@ const char* mtar_strerror(int err);
6568
int mtar_open(mtar_t *tar, const char *filename, const char *mode);
6669
int mtar_close(mtar_t *tar);
6770

68-
int mtar_seek(mtar_t *tar, unsigned pos);
71+
int mtar_seek(mtar_t *tar, mtar_size_t pos);
6972
int mtar_rewind(mtar_t *tar);
7073
int mtar_next(mtar_t *tar);
7174
int mtar_find(mtar_t *tar, const char *name, mtar_header_t *h);
7275
int mtar_read_header(mtar_t *tar, mtar_header_t *h);
73-
int mtar_read_data(mtar_t *tar, void *ptr, unsigned size);
76+
int mtar_read_data(mtar_t *tar, void *ptr, mtar_size_t size);
7477

7578
int mtar_write_header(mtar_t *tar, const mtar_header_t *h);
76-
int mtar_write_file_header(mtar_t *tar, const char *name, unsigned size);
79+
int mtar_write_file_header(mtar_t *tar, const char *name, mtar_size_t size);
7780
int mtar_write_dir_header(mtar_t *tar, const char *name);
78-
int mtar_write_data(mtar_t *tar, const void *data, unsigned size);
81+
int mtar_write_data(mtar_t *tar, const void *data, mtar_size_t size);
7982
int mtar_finalize(mtar_t *tar);
8083

8184

0 commit comments

Comments
 (0)