Skip to content

Commit b500721

Browse files
kbleesgitster
authored andcommitted
pack-bitmap: do not use gcc packed attribute
The "__attribute__" flag may be a noop on some compilers. That's OK as long as the code is correct without the attribute, but in this case it is not. We would typically end up with a struct that is 2 bytes too long due to struct padding, breaking both reading and writing of bitmaps. Instead of marshalling the data in a struct, let's just provide helpers for reading and writing the appropriate types. Besides being correct on all platforms, the result is more efficient and simpler to read. Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 68f4e1f commit b500721

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

csum-file.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,15 @@ extern void sha1flush(struct sha1file *f);
3939
extern void crc32_begin(struct sha1file *);
4040
extern uint32_t crc32_end(struct sha1file *);
4141

42+
static inline void sha1write_u8(struct sha1file *f, uint8_t data)
43+
{
44+
sha1write(f, &data, sizeof(data));
45+
}
46+
47+
static inline void sha1write_be32(struct sha1file *f, uint32_t data)
48+
{
49+
data = htonl(data);
50+
sha1write(f, &data, sizeof(data));
51+
}
52+
4253
#endif

pack-bitmap-write.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,19 +473,17 @@ static void write_selected_commits_v1(struct sha1file *f,
473473

474474
for (i = 0; i < writer.selected_nr; ++i) {
475475
struct bitmapped_commit *stored = &writer.selected[i];
476-
struct bitmap_disk_entry on_disk;
477476

478477
int commit_pos =
479478
sha1_pos(stored->commit->object.sha1, index, index_nr, sha1_access);
480479

481480
if (commit_pos < 0)
482481
die("BUG: trying to write commit not in index");
483482

484-
on_disk.object_pos = htonl(commit_pos);
485-
on_disk.xor_offset = stored->xor_offset;
486-
on_disk.flags = stored->flags;
483+
sha1write_be32(f, commit_pos);
484+
sha1write_u8(f, stored->xor_offset);
485+
sha1write_u8(f, stored->flags);
487486

488-
sha1write(f, &on_disk, sizeof(on_disk));
489487
dump_bitmap(f, stored->write_as);
490488
}
491489
}

pack-bitmap.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,24 @@ static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
197197
return stored;
198198
}
199199

200+
static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
201+
{
202+
uint32_t result = get_be32(buffer + *pos);
203+
(*pos) += sizeof(result);
204+
return result;
205+
}
206+
207+
static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
208+
{
209+
return buffer[(*pos)++];
210+
}
211+
200212
static int load_bitmap_entries_v1(struct bitmap_index *index)
201213
{
202214
static const size_t MAX_XOR_OFFSET = 160;
203215

204216
uint32_t i;
205217
struct stored_bitmap **recent_bitmaps;
206-
struct bitmap_disk_entry *entry;
207218

208219
recent_bitmaps = xcalloc(MAX_XOR_OFFSET, sizeof(struct stored_bitmap));
209220

@@ -214,15 +225,12 @@ static int load_bitmap_entries_v1(struct bitmap_index *index)
214225
uint32_t commit_idx_pos;
215226
const unsigned char *sha1;
216227

217-
entry = (struct bitmap_disk_entry *)(index->map + index->map_pos);
218-
index->map_pos += sizeof(struct bitmap_disk_entry);
228+
commit_idx_pos = read_be32(index->map, &index->map_pos);
229+
xor_offset = read_u8(index->map, &index->map_pos);
230+
flags = read_u8(index->map, &index->map_pos);
219231

220-
commit_idx_pos = ntohl(entry->object_pos);
221232
sha1 = nth_packed_object_sha1(index->pack, commit_idx_pos);
222233

223-
xor_offset = (int)entry->xor_offset;
224-
flags = (int)entry->flags;
225-
226234
bitmap = read_bitmap_1(index);
227235
if (!bitmap)
228236
return -1;

pack-bitmap.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
#include "khash.h"
66
#include "pack-objects.h"
77

8-
struct bitmap_disk_entry {
9-
uint32_t object_pos;
10-
uint8_t xor_offset;
11-
uint8_t flags;
12-
} __attribute__((packed));
13-
148
struct bitmap_disk_header {
159
char magic[4];
1610
uint16_t version;

0 commit comments

Comments
 (0)