Skip to content

Commit

Permalink
Use size_t instead of 'unsigned long' for data in memory
Browse files Browse the repository at this point in the history
Currently the length of data which is stored in memory is stored
in "unsigned long" at many places in the code base.
This is OK when both "unsigned long" and size_t are 32 bits,
(and is OK when both are 64 bits).
On a 64 bit Windows system am "unsigned long" is 32 bit, and
that may be too short to measure the size of objects in memory,
a size_t is the natural choice.

Improve the code base in "small steps", as small as possible.
The smallest step seems to be much bigger than expected.
See this code-snippet from convert.c:
        const char *ret;
        unsigned long sz;
        void *data = read_blob_data_from_index(istate, path, &sz);
        ret = gather_convert_stats_ascii(data, sz);

The corrected version looks like this:
        const char *ret;
        size_t sz;
        void *data = read_blob_data_from_index(istate, path, &sz);
        ret = gather_convert_stats_ascii(data, sz);

However, when the Git code base is compiled with a compiler that
complains that "unsigned long" is different from size_t, we end
up in this huge patch, before the code base cleanly compiles.

Signed-off-by: Torsten Bögershausen <[email protected]>
  • Loading branch information
tboegi authored and t-b committed Jan 30, 2019
1 parent 1ec976b commit 7557481
Show file tree
Hide file tree
Showing 85 changed files with 431 additions and 428 deletions.
78 changes: 39 additions & 39 deletions apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ static void set_default_whitespace_mode(struct apply_state *state)
* of context lines.
*/
struct fragment {
unsigned long leading, trailing;
unsigned long oldpos, oldlines;
unsigned long newpos, newlines;
size_t leading, trailing;
size_t oldpos, oldlines;
size_t newpos, newlines;
/*
* 'patch' is usually borrowed from buf in apply_patch(),
* but some codepaths store an allocated buffer.
Expand Down Expand Up @@ -419,9 +419,9 @@ static int read_patch_file(struct strbuf *sb, int fd)
return 0;
}

static unsigned long linelen(const char *buffer, unsigned long size)
static size_t linelen(const char *buffer, size_t size)
{
unsigned long len = 0;
size_t len = 0;
while (size--) {
len++;
if (*buffer++ == '\n')
Expand Down Expand Up @@ -1318,7 +1318,7 @@ static int parse_git_header(struct apply_state *state,
unsigned int size,
struct patch *patch)
{
unsigned long offset;
size_t offset;

/* A git diff has explicit new/delete information, so we don't guess */
patch->is_new = 0;
Expand Down Expand Up @@ -1388,7 +1388,7 @@ static int parse_git_header(struct apply_state *state,
return offset;
}

static int parse_num(const char *line, unsigned long *p)
static int parse_num(const char *line, size_t *p)
{
char *ptr;

Expand All @@ -1399,7 +1399,7 @@ static int parse_num(const char *line, unsigned long *p)
}

static int parse_range(const char *line, int len, int offset, const char *expect,
unsigned long *p1, unsigned long *p2)
size_t *p1, size_t *p2)
{
int digits, ex;

Expand Down Expand Up @@ -1514,19 +1514,19 @@ static int parse_fragment_header(const char *line, int len, struct fragment *fra
*/
static int find_header(struct apply_state *state,
const char *line,
unsigned long size,
size_t size,
int *hdrsize,
struct patch *patch)
{
unsigned long offset, len;
size_t offset, len;

patch->is_toplevel_relative = 0;
patch->is_rename = patch->is_copy = 0;
patch->is_new = patch->is_delete = -1;
patch->old_mode = patch->new_mode = 0;
patch->old_name = patch->new_name = NULL;
for (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) {
unsigned long nextlen;
size_t nextlen;

len = linelen(line, size);
if (!len)
Expand Down Expand Up @@ -1664,14 +1664,14 @@ static void check_old_for_crlf(struct patch *patch, const char *line, int len)
*/
static int parse_fragment(struct apply_state *state,
const char *line,
unsigned long size,
size_t size,
struct patch *patch,
struct fragment *fragment)
{
int added, deleted;
int len = linelen(line, size), offset;
unsigned long oldlines, newlines;
unsigned long leading, trailing;
size_t oldlines, newlines;
size_t leading, trailing;

offset = parse_fragment_header(line, len, fragment);
if (offset < 0)
Expand Down Expand Up @@ -1786,11 +1786,11 @@ static int parse_fragment(struct apply_state *state,
*/
static int parse_single_patch(struct apply_state *state,
const char *line,
unsigned long size,
size_t size,
struct patch *patch)
{
unsigned long offset = 0;
unsigned long oldlines = 0, newlines = 0, context = 0;
size_t offset = 0;
size_t oldlines = 0, newlines = 0, context = 0;
struct fragment **fragp = &patch->fragments;

while (size > 4 && !memcmp(line, "@@ -", 4)) {
Expand Down Expand Up @@ -1861,8 +1861,8 @@ static inline int metadata_changes(struct patch *patch)
patch->old_mode != patch->new_mode);
}

static char *inflate_it(const void *data, unsigned long size,
unsigned long inflated_size)
static char *inflate_it(const void *data, size_t size,
size_t inflated_size)
{
git_zstream stream;
void *out;
Expand Down Expand Up @@ -1891,7 +1891,7 @@ static char *inflate_it(const void *data, unsigned long size,
*/
static struct fragment *parse_binary_hunk(struct apply_state *state,
char **buf_p,
unsigned long *sz_p,
size_t *sz_p,
int *status_p,
int *used_p)
{
Expand All @@ -1908,10 +1908,10 @@ static struct fragment *parse_binary_hunk(struct apply_state *state,
* to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
*/
int llen, used;
unsigned long size = *sz_p;
size_t size = *sz_p;
char *buffer = *buf_p;
int patch_method;
unsigned long origlen;
size_t origlen;
char *data = NULL;
int hunk_size = 0;
struct fragment *frag;
Expand Down Expand Up @@ -2003,7 +2003,7 @@ static struct fragment *parse_binary_hunk(struct apply_state *state,
*/
static int parse_binary(struct apply_state *state,
char *buffer,
unsigned long size,
size_t size,
struct patch *patch)
{
/*
Expand Down Expand Up @@ -2120,7 +2120,7 @@ static int use_patch(struct apply_state *state, struct patch *p)
* the number of bytes consumed otherwise,
* so that the caller can call us again for the next patch.
*/
static int parse_chunk(struct apply_state *state, char *buffer, unsigned long size, struct patch *patch)
static int parse_chunk(struct apply_state *state, char *buffer, size_t size, struct patch *patch)
{
int hdrsize, patchsize;
int offset = find_header(state, buffer, size, &hdrsize, patch);
Expand Down Expand Up @@ -2150,7 +2150,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
if (!patchsize) {
static const char git_binary[] = "GIT binary patch\n";
int hd = hdrsize + offset;
unsigned long llen = linelen(buffer + hd, size - hd);
size_t llen = linelen(buffer + hd, size - hd);

if (llen == sizeof(git_binary) - 1 &&
!memcmp(git_binary, buffer + hd, llen)) {
Expand Down Expand Up @@ -2394,7 +2394,7 @@ static void update_pre_post_images(struct image *preimage,
static int line_by_line_fuzzy_match(struct image *img,
struct image *preimage,
struct image *postimage,
unsigned long current,
size_t current,
int current_lno,
int preimage_limit)
{
Expand Down Expand Up @@ -2463,7 +2463,7 @@ static int match_fragment(struct apply_state *state,
struct image *img,
struct image *preimage,
struct image *postimage,
unsigned long current,
size_t current,
int current_lno,
unsigned ws_rule,
int match_beginning, int match_end)
Expand Down Expand Up @@ -2674,7 +2674,7 @@ static int find_pos(struct apply_state *state,
int match_beginning, int match_end)
{
int i;
unsigned long backwards, forwards, current;
size_t backwards, forwards, current;
int backwards_lno, forwards_lno, current_lno;

/*
Expand Down Expand Up @@ -2848,7 +2848,7 @@ static int apply_one_fragment(struct apply_state *state,
int new_blank_lines_at_end = 0;
int found_new_blank_lines_at_end = 0;
int hunk_linenr = frag->linenr;
unsigned long leading, trailing;
size_t leading, trailing;
int pos, applied_pos;
struct image preimage;
struct image postimage;
Expand Down Expand Up @@ -3072,9 +3072,9 @@ static int apply_one_fragment(struct apply_state *state,
*/
if ((leading != frag->leading ||
trailing != frag->trailing) && state->apply_verbosity > verbosity_silent)
fprintf_ln(stderr, _("Context reduced to (%ld/%ld)"
fprintf_ln(stderr, _("Context reduced to (%"PRIuMAX"/%"PRIuMAX")"
" to apply fragment at %d"),
leading, trailing, applied_pos+1);
(uintmax_t)leading, (uintmax_t)trailing, applied_pos+1);
update_image(state, img, applied_pos, &preimage, &postimage);
} else {
if (state->apply_verbosity > verbosity_normal)
Expand All @@ -3096,7 +3096,7 @@ static int apply_binary_fragment(struct apply_state *state,
struct patch *patch)
{
struct fragment *fragment = patch->fragments;
unsigned long len;
size_t len;
void *dst;

if (!fragment)
Expand Down Expand Up @@ -3186,7 +3186,7 @@ static int apply_binary(struct apply_state *state,
if (has_sha1_file(oid.hash)) {
/* We already have the postimage */
enum object_type type;
unsigned long size;
size_t size;
char *result;

result = read_object_file(&oid, &type, &size);
Expand Down Expand Up @@ -3231,7 +3231,7 @@ static int apply_fragments(struct apply_state *state, struct image *img, struct
while (frag) {
nth++;
if (apply_one_fragment(state, img, frag, inaccurate_eof, ws_rule, nth)) {
error(_("patch failed: %s:%ld"), name, frag->oldpos);
error(_("patch failed: %s:%"PRIuMAX), name, (uintmax_t)frag->oldpos);
if (!state->apply_with_reject)
return -1;
frag->rejected = 1;
Expand All @@ -3248,7 +3248,7 @@ static int read_blob_object(struct strbuf *buf, const struct object_id *oid, uns
strbuf_addf(buf, "Subproject commit %s\n", oid_to_hex(oid));
} else {
enum object_type type;
unsigned long sz;
size_t sz;
char *result;

result = read_object_file(oid, &type, &sz);
Expand Down Expand Up @@ -4276,7 +4276,7 @@ static int add_index_file(struct apply_state *state,
const char *path,
unsigned mode,
void *buf,
unsigned long size)
size_t size)
{
struct stat st;
struct cache_entry *ce;
Expand Down Expand Up @@ -4330,7 +4330,7 @@ static int add_index_file(struct apply_state *state,
*/
static int try_create_file(struct apply_state *state, const char *path,
unsigned int mode, const char *buf,
unsigned long size)
size_t size)
{
int fd, res;
struct strbuf nbuf = STRBUF_INIT;
Expand Down Expand Up @@ -4381,7 +4381,7 @@ static int create_one_file(struct apply_state *state,
char *path,
unsigned mode,
const char *buf,
unsigned long size)
size_t size)
{
int res;

Expand Down Expand Up @@ -4473,7 +4473,7 @@ static int create_file(struct apply_state *state, struct patch *patch)
{
char *path = patch->new_name;
unsigned mode = patch->new_mode;
unsigned long size = patch->resultsize;
size_t size = patch->resultsize;
char *buf = patch->result;

if (!mode)
Expand Down
18 changes: 9 additions & 9 deletions archive-tar.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define BLOCKSIZE (RECORDSIZE * 20)

static char block[BLOCKSIZE];
static unsigned long offset;
static size_t offset;

static int tar_umask = 002;

Expand Down Expand Up @@ -51,12 +51,12 @@ static void write_if_needed(void)
* queues up writes, so that all our write(2) calls write exactly one
* full block; pads writes to RECORDSIZE
*/
static void do_write_blocked(const void *data, unsigned long size)
static void do_write_blocked(const void *data, size_t size)
{
const char *buf = data;

if (offset) {
unsigned long chunk = BLOCKSIZE - offset;
size_t chunk = BLOCKSIZE - offset;
if (size < chunk)
chunk = size;
memcpy(block + offset, buf, chunk);
Expand All @@ -78,7 +78,7 @@ static void do_write_blocked(const void *data, unsigned long size)

static void finish_record(void)
{
unsigned long tail;
size_t tail;
tail = offset % RECORDSIZE;
if (tail) {
memset(block + offset, 0, RECORDSIZE - tail);
Expand All @@ -87,7 +87,7 @@ static void finish_record(void)
write_if_needed();
}

static void write_blocked(const void *data, unsigned long size)
static void write_blocked(const void *data, size_t size)
{
do_write_blocked(data, size);
finish_record();
Expand Down Expand Up @@ -116,7 +116,7 @@ static int stream_blocked(const struct object_id *oid)
{
struct git_istream *st;
enum object_type type;
unsigned long sz;
size_t sz;
char buf[BLOCKSIZE];
ssize_t readlen;

Expand Down Expand Up @@ -199,7 +199,7 @@ static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)

static void prepare_header(struct archiver_args *args,
struct ustar_header *header,
unsigned int mode, unsigned long size)
unsigned int mode, size_t size)
{
xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
xsnprintf(header->size, sizeof(header->size), "%011"PRIoMAX , S_ISREG(mode) ? (uintmax_t)size : (uintmax_t)0);
Expand All @@ -220,7 +220,7 @@ static void prepare_header(struct archiver_args *args,

static void write_extended_header(struct archiver_args *args,
const struct object_id *oid,
const void *buffer, unsigned long size)
const void *buffer, size_t size)
{
struct ustar_header header;
unsigned int mode;
Expand All @@ -241,7 +241,7 @@ static int write_tar_entry(struct archiver_args *args,
struct ustar_header header;
struct strbuf ext_header = STRBUF_INIT;
unsigned int old_mode = mode;
unsigned long size, size_in_header;
size_t size, size_in_header;
void *buffer;
int err = 0;

Expand Down
2 changes: 1 addition & 1 deletion archive-zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ static int write_zip_entry(struct archiver_args *args,
void *buffer;
struct git_istream *stream = NULL;
unsigned long flags = 0;
unsigned long size;
size_t size;
int is_binary = -1;
const char *path_without_prefix = path + args->baselen;
unsigned int creator_version = 0;
Expand Down
2 changes: 1 addition & 1 deletion archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static void format_subst(const struct commit *commit,
void *object_file_to_archive(const struct archiver_args *args,
const char *path, const struct object_id *oid,
unsigned int mode, enum object_type *type,
unsigned long *sizep)
size_t *sizep)
{
void *buffer;
const struct commit *commit = args->convert ? args->commit : NULL;
Expand Down
2 changes: 1 addition & 1 deletion archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ extern int write_archive_entries(struct archiver_args *args, write_archive_entry
extern void *object_file_to_archive(const struct archiver_args *args,
const char *path, const struct object_id *oid,
unsigned int mode, enum object_type *type,
unsigned long *sizep);
size_t *sizep);

#endif /* ARCHIVE_H */
Loading

0 comments on commit 7557481

Please sign in to comment.