Fix git to be (more) ANSI C99 compliant.
PATCH 1/1 Fix git to be (more) ANSI C99 compliant.
Using this patch I was able to build git with
$ make CFLAGS="-Wall -Werror -ansi -pedantic -std=c99 -D_XOPEN_SOURCE=500 -D_BSD_SOURCE"
While most of this patch fixes void-pointer arithmetic and is therefore trivial, I had to change the use of a struct with FAMs in `diff-lib.c'. Since this is the first time I encountered FAMs it'd probably be a good idea if someone who knows would take a look at that.
---
blame.c | 4 ++-- builtin-apply.c | 6 +++--- builtin-read-tree.c | 3 +-- builtin-tar-tree.c | 6 +++--- convert-objects.c | 22 +++++++++++----------- csum-file.c | 4 ++-- diff-delta.c | 5 +++-- diff-lib.c | 41 ++++++++++++++++++++++------------------- diff.c | 2 +- diffcore-order.c | 2 +- http-fetch.c | 15 ++++++++------- http-push.c | 15 ++++++++------- http.c | 4 ++-- imap-send.c | 2 +- pack-check.c | 6 +++--- pack-objects.c | 4 ++-- pack-redundant.c | 16 ++++++++-------- patch-delta.c | 4 ++-- pkt-line.c | 4 ++-- quote.c | 9 ++++++++- read-cache.c | 13 +++++++------ sha1_file.c | 27 ++++++++++++++------------- ssh-fetch.c | 2 +- tag.c | 4 ++-- tree-walk.c | 11 ++++++----- 25 files changed, 123 insertions(+), 108 deletions(-)
714b7eca861ad3b6863209f0298d55c0c2292116
blame.c +2 -2
@@ -301,9 +301,9 @@ static void fill_line_map(struct commitif (DEBUG )printf ("map: i1: %d %d %p i2: %d %d %p\n" , i1, map[i1], - i1 !=-1 ? blame_lines[map[i1]] :NULL , + (void *) (i1 !=-1 ? blame_lines[map[i1]] :NULL ), i2, map2[i2], - i2 !=-1 ? blame_lines[map2[i2]] :NULL ); + (void *) (i2 !=-1 ? blame_lines[map2[i2]] :NULL ));if (map2[i2] !=-1 && blame_lines[map[i1]] && !blame_lines[map2[i2]])
builtin-apply.c +3 -3
@@ -148,7 +148,7 @@ static void *read_patch_file(int fd, uns buffer =xrealloc (buffer, alloc); nr = alloc - size; } - nr =xread (fd, buffer + size, nr); + nr =xread (fd, (char *) buffer + size, nr);if (!nr)break ;if (nr <0 )
@@ -164,7 +164,7 @@ static void *read_patch_file(int fd, uns*/ if (alloc < size +SLOP ) buffer =xrealloc (buffer, size +SLOP ); -memset (buffer + size,0 ,SLOP ); +memset ((char *) buffer + size,0 ,SLOP );return buffer; }
@@ -1194,7 +1194,7 @@ static int read_old_data(struct stat *streturn error ("unable to open %s" , path); got =0 ;for (;;) { -int ret =xread (fd, buf + got, size - got); +int ret =xread (fd, (char *) buf + got, size - got);if (ret <=0 )break ; got += ret;
builtin-read-tree.c +1 -2
@@ -30,8 +30,7 @@ static int merge_size = 0;static struct object_list *trees =NULL ; -static struct cache_entry df_conflict_entry = { -}; +static struct cache_entry df_conflict_entry;struct tree_entry_list {struct tree_entry_list *next;
builtin-tar-tree.c +3 -3
@@ -34,7 +34,7 @@ static void reliable_write(void *buf, undie ("git-tar-tree: disk full?" ); } size -= ret; - buf += ret; + buf = (char *) buf + ret; } }
@@ -87,13 +87,13 @@ static void write_blocked(void *buf, unsmemcpy (block + offset, buf, chunk); size -= chunk; offset += chunk; - buf += chunk; + buf = (char *) buf + chunk;write_if_needed (); }while (size >=BLOCKSIZE ) {reliable_write (buf,BLOCKSIZE ); size -=BLOCKSIZE ; - buf +=BLOCKSIZE ; + buf = (char *) buf +BLOCKSIZE ; }if (size) {memcpy (block + offset, buf, size);
convert-objects.c +11 -11
@@ -103,12 +103,12 @@ static int write_subdirectory(void *buffif (!slash) { newlen +=sprintf (new + newlen,"%o %s" , mode, path); new[newlen++] ='\0' ; -memcpy (new + newlen, buffer + len -20 ,20 ); +memcpy (new + newlen, (char *) buffer + len -20 ,20 ); newlen +=20 ; used += len; size -= len; - buffer += len; + buffer = (char *) buffer + len;continue ; }
@@ -121,7 +121,7 @@ static int write_subdirectory(void *buff used += len; size -= len; - buffer += len; + buffer = (char *) buffer + len; }write_sha1_file (new, newlen, tree_type, result_sha1);
@@ -137,13 +137,13 @@ static void convert_tree(void *buffer, uwhile (size) {int len =1 +strlen (buffer); -convert_binary_sha1 (buffer + len); +convert_binary_sha1 ((char *) buffer + len); len +=20 ;if (len > size)die ("corrupt tree object" ); size -= len; - buffer += len; + buffer = (char *) buffer + len; }write_subdirectory (orig_buffer, orig_size,"" ,0 , result_sha1);
@@ -244,14 +244,14 @@ static void convert_date(void *buffer, u// "tree <sha1>\n" memcpy (new + newlen, buffer,46 ); newlen +=46 ; - buffer +=46 ; + buffer = (char *) buffer +46 ; size -=46 ;// "parent <sha1>\n" while (!memcmp (buffer,"parent " ,7 )) {memcpy (new + newlen, buffer,48 ); newlen +=48 ; - buffer +=48 ; + buffer = (char *) buffer +48 ; size -=48 ; }
@@ -275,11 +275,11 @@ static void convert_commit(void *buffer,if (memcmp (buffer,"tree " ,5 ))die ("Bad commit '%s'" , (char *) buffer); -convert_ascii_sha1 (buffer+5 ); - buffer +=46 ;/* "tree " + "hex sha1" + "\n" */ +convert_ascii_sha1 ((char *) buffer +5 ); + buffer = (char *) buffer +46 ;/* "tree " + "hex sha1" + "\n" */ while (!memcmp (buffer,"parent " ,7 )) { -convert_ascii_sha1 (buffer+7 ); - buffer +=48 ; +convert_ascii_sha1 ((char *) buffer +7 ); + buffer = (char *) buffer +48 ; }convert_date (orig_buffer, orig_size, result_sha1); }
csum-file.c +2 -2
@@ -17,7 +17,7 @@ static int sha1flush(struct sha1file *f,for (;;) {int ret =xwrite (f->fd, buf, count);if (ret >0 ) { - buf += ret; + buf = (char *) buf + ret; count -= ret;if (count)continue ;
@@ -57,7 +57,7 @@ int sha1write(struct sha1file *f, void *memcpy (f->buffer + offset, buf, nr); count -= nr; offset += nr; - buf += nr; + buf = (char *) buf + nr; left -= nr;if (!left) {SHA1_Update (&f->ctx, f->buffer, offset);
diff-delta.c +3 -2
@@ -22,6 +22,7 @@ #include <stdlib.h>#include <string.h> #include "delta.h" +#include "git-compat-util.h" /* maximum hash entry list for the same hash bucket */ #define HASH_LIMIT 64
@@ -131,7 +132,7 @@ struct delta_index {const void *src_buf;unsigned long src_size;unsigned int hash_mask; -struct index_entry *hash[0 ]; +struct index_entry *hash[FLEX_ARRAY ]; };struct delta_index *create_delta_index (const void *buf,unsigned long bufsize)
@@ -283,7 +284,7 @@ create_delta(const struct delta_index *i ref_data = index->src_buf; ref_top = ref_data + index->src_size; data = trg_buf; - top = trg_buf + trg_size; + top = (const unsigned char *) trg_buf + trg_size; outpos++; val =0 ;
diff-lib.c +22 -19
67 lines not on this page · raw message
diff.c +1 -1
@@ -515,7 +515,7 @@ static void emit_binary_diff(mmfile_t *oelse line[0 ] = bytes -26 +'a' -1 ;encode_85 (line +1 , cp, bytes); - cp += bytes; + cp = (char *) cp + bytes;puts (line); }printf ("\n" );
diffcore-order.c +1 -1
@@ -30,7 +30,7 @@ static void prepare_order(const char *orclose (fd);if (map ==MAP_FAILED )return ; - endp = map + st.st_size; + endp = (char *) map + st.st_size;for (pass =0 ; pass <2 ; pass++) { cnt =0 ; cp = map;
http-fetch.c +8 -7
@@ -123,7 +123,7 @@ static size_t fwrite_sha1_file(void *ptrstruct object_request *obj_req = (struct object_request *)data; do { ssize_t retval = write(obj_req->local, - ptr + posn, size - posn); + (char *) ptr + posn, size - posn); if (retval < 0) return posn; posn += retval;
@@ -1136,13 +1136,14 @@ int fetch(unsigned char *sha1)static inline int needs_quote (int ch) { -switch (ch) { -case '/' :case '-' :case '.' : - case 'A'...'Z': case 'a'...'z': case '0'...'9': +if (((ch >='A' ) && (ch <='Z' )) + || ((ch >='a' ) && (ch <='z' )) + || ((ch >='0' ) && (ch <='9' )) + || (ch =='/' ) + || (ch =='-' ) + || (ch =='.' ))return 0 ; -default : -return 1 ; - } +return 1 ; }static inline int hex (int v)
http-push.c +8 -7
@@ -196,7 +196,7 @@ static size_t fwrite_sha1_file(void *ptrstruct transfer_request *request = (struct transfer_request *)data; do { ssize_t retval = write(request->local_fileno, - ptr + posn, size - posn); + (char *) ptr + posn, size - posn); if (retval < 0) return posn; posn += retval;
@@ -1077,13 +1077,14 @@ static int fetch_indices(void)static inline int needs_quote (int ch) { -switch (ch) { -case '/' :case '-' :case '.' : - case 'A'...'Z': case 'a'...'z': case '0'...'9': +if (((ch >='A' ) && (ch <='Z' )) + || ((ch >='a' ) && (ch <='z' )) + || ((ch >='0' ) && (ch <='9' )) + || (ch =='/' ) + || (ch =='-' ) + || (ch =='.' ))return 0 ; -default : -return 1 ; - } +return 1 ; }static inline int hex (int v)
http.c +2 -2
@@ -34,7 +34,7 @@ size_t fread_buffer(void *ptr, size_t elsize_t size = eltsize * nmemb;if (size > buffer->size - buffer->posn) size = buffer->size - buffer->posn; -memcpy (ptr, buffer->buffer + buffer->posn, size); +memcpy (ptr, (char *) buffer->buffer + buffer->posn, size); buffer->posn += size;return size; }
@@ -49,7 +49,7 @@ size_t fwrite_buffer(const void *ptr, si buffer->size = buffer->posn + size; buffer->buffer =xrealloc (buffer->buffer, buffer->size); } -memcpy (buffer->buffer + buffer->posn, ptr, size); +memcpy ((char *) buffer->buffer + buffer->posn, ptr, size); buffer->posn += size; data_received++;return size;
imap-send.c +1 -1
@@ -93,7 +93,7 @@ typedef struct {char *data;int len;unsigned char flags; - unsigned char crlf:1; + unsigned int crlf:1; } msg_data_t;#define DRV_OK 0
pack-check.c +3 -3
@@ -29,10 +29,10 @@ static int verify_packfile(struct packed pack_base = p->pack_base;SHA1_Update (&ctx, pack_base, pack_size -20 );SHA1_Final (sha1, &ctx); -if (memcmp (sha1, pack_base + pack_size -20 ,20 )) +if (memcmp (sha1, (char *) pack_base + pack_size -20 ,20 ))return error ("Packfile %s SHA1 mismatch with itself" , p->pack_name); -if (memcmp (sha1, index_base + index_size -40 ,20 )) +if (memcmp (sha1, (char *) index_base + index_size -40 ,20 ))return error ("Packfile %s SHA1 mismatch with idx" , p->pack_name);
@@ -135,7 +135,7 @@ int verify_pack(struct packed_git *p, inSHA1_Init (&ctx);SHA1_Update (&ctx, index_base, index_size -20 );SHA1_Final (sha1, &ctx); -if (memcmp (sha1, index_base + index_size -20 ,20 )) +if (memcmp (sha1, (char *) index_base + index_size -20 ,20 )) ret =error ("Packfile index for %s SHA1 mismatch" , p->pack_name);
pack-objects.c +2 -2
@@ -156,7 +156,7 @@ static void prepare_pack_revindex(struct rix->revindex =xmalloc (sizeof (unsigned long ) * (num_ent +1 ));for (i =0 ; i < num_ent; i++) { -unsigned int hl = *((unsigned int *)(index +24 * i)); +unsigned int hl = *((unsigned int *)((char *) index +24 *i)); rix->revindex[i] =ntohl (hl); }/* This knows the pack format -- the 20-byte trailer
@@ -300,7 +300,7 @@ static unsigned long write_object(structuse_packed_git (p); datalen =find_packed_object_size (p, entry->in_pack_offset); - buf = p->pack_base + entry->in_pack_offset; + buf = (char *) p->pack_base + entry->in_pack_offset;sha1write (f, buf, datalen);unuse_packed_git (p); hdrlen =0 ;/* not really */
pack-redundant.c +8 -8
@@ -246,12 +246,12 @@ static struct pack_list * pack_list_diffstatic void cmp_two_packs (struct pack_list *p1,struct pack_list *p2) {int p1_off, p2_off; -void *p1_base, *p2_base; +unsigned char *p1_base, *p2_base;struct llist_item *p1_hint =NULL , *p2_hint =NULL ; p1_off = p2_off =256 *4 +4 ; - p1_base = (void *)p1->pack->index_base; - p2_base = (void *)p2->pack->index_base; + p1_base = (unsigned char *) p1->pack->index_base; + p2_base = (unsigned char *) p2->pack->index_base;while (p1_off <= p1->pack->index_size -3 *20 && p2_off <= p2->pack->index_size -3 *20 )
@@ -351,11 +351,11 @@ static size_t sizeof_union(struct packed {size_t ret =0 ;int p1_off, p2_off; -void *p1_base, *p2_base; +char *p1_base, *p2_base; p1_off = p2_off =256 *4 +4 ; - p1_base = (void *)p1->index_base; - p2_base = (void *)p2->index_base; + p1_base = (char *)p1->index_base; + p2_base = (char *)p2->index_base;while (p1_off <= p1->index_size -3 *20 && p2_off <= p2->index_size -3 *20 )
@@ -534,7 +534,7 @@ static struct pack_list * add_pack(struc {struct pack_list l;size_t off; -void *base; +unsigned char *base;if (!p->pack_local && !(alt_odb || verbose))return NULL ;
@@ -543,7 +543,7 @@ static struct pack_list * add_pack(strucllist_init (&l.all_objects); off =256 *4 +4 ; - base = (void *)p->index_base; + base = (unsigned char *)p->index_base;while (off <= p->index_size -3 *20 ) {llist_insert_back (l.all_objects, base + off); off +=24 ;
patch-delta.c +2 -2
@@ -25,7 +25,7 @@ void *patch_delta(const void *src_buf, ureturn NULL ; data = delta_buf; - top = delta_buf + delta_size; + top = (const unsigned char *) delta_buf + delta_size;/* make sure the orig file size matches what we expect */ size =get_delta_hdr_size (&data, top);
@@ -56,7 +56,7 @@ void *patch_delta(const void *src_buf, u cp_off + cp_size > src_size || cp_size > size) goto bad; -memcpy (out, src_buf + cp_off, cp_size); +memcpy (out, (char *) src_buf + cp_off, cp_size); out += cp_size; size -= cp_size; }else if (cmd) {
pkt-line.c +2 -2
@@ -21,7 +21,7 @@ static void safe_write(int fd, const voiwhile (n) {int ret =xwrite (fd, buf, n);if (ret >0 ) { - buf += ret; + buf = (char *) buf + ret; n -= ret;continue ; }
@@ -66,7 +66,7 @@ static void safe_read(int fd, void *buffint n =0 ;while (n < size) { -int ret =xread (fd, buffer + n, size - n); +int ret =xread (fd, (char *) buffer + n, size - n);if (ret <0 )die ("read error (%s)" ,strerror (errno));if (!ret)
quote.c +8 -1
@@ -206,7 +206,14 @@ #define EMIT(c) (outp ? (*outp++ = (c))case '\\' :case '"' :break ;/* verbatim */ - case '0'...'7': +case '0' : +case '1' : +case '2' : +case '3' : +case '4' : +case '5' : +case '6' : +case '7' :/* octal */ ac = ((ch -'0' ) <<6 );if ((ch = *sp++) <'0' ||'7' < ch)
read-cache.c +7 -6
@@ -706,7 +706,7 @@ static int verify_hdr(struct cache_headeSHA1_Init (&c);SHA1_Update (&c, hdr, size -20 );SHA1_Final (sha1, &c); -if (memcmp (sha1, (void *)hdr + size -20 ,20 )) +if (memcmp (sha1, (char *) hdr + size -20 ,20 ))return error ("bad index file sha1 signature" );return 0 ; }
@@ -770,7 +770,7 @@ int read_cache(void) offset =sizeof (*hdr);for (i =0 ; i < active_nr; i++) { -struct cache_entry *ce = map + offset; +struct cache_entry *ce = (struct cache_entry *) ((char *) map + offset); offset = offset +ce_size (ce); active_cache[i] = ce; }
@@ -783,10 +783,11 @@ int read_cache(void)* in 4-byte network byte order. */ unsigned long extsize; -memcpy (&extsize, map + offset +4 ,4 ); +memcpy (&extsize, (char *) map + offset +4 ,4 ); extsize =ntohl (extsize); -if (read_index_extension (map + offset, - map + offset +8 , extsize) <0 ) +if (read_index_extension (((const char *) map) + offset, + (char *) map + offset +8 , + extsize) <0 ) goto unmap; offset +=8 ; offset += extsize;
@@ -820,7 +821,7 @@ static int ce_write(SHA_CTX *context, in } write_buffer_len = buffered; len -= partial; - data += partial; + data = (char *) data + partial; }return 0 ; }
sha1_file.c +14 -13
@@ -486,7 +486,8 @@ int use_packed_git(struct packed_git *p)* this is cheap. */ if (memcmp ((char *)(p->index_base) + p->index_size -40 , - p->pack_base + p->pack_size -20 ,20 )) { + (char *) p->pack_base + p->pack_size -20 , +20 )) {die ("packfile %s does not match index." , p->pack_name); }
@@ -701,7 +702,7 @@ static void *unpack_sha1_rest(z_stream *int bytes =strlen (buffer) +1 ;unsigned char *buf =xmalloc (1 +size); -memcpy (buf, buffer + bytes, stream->total_out - bytes); +memcpy (buf, (char *) buffer + bytes, stream->total_out - bytes); bytes = stream->total_out - bytes;if (bytes < size) { stream->next_out = buf + bytes;
@@ -853,7 +854,7 @@ static unsigned long unpack_object_headeif (offset >= p->pack_size)die ("object offset outside of pack file" ); - pack = p->pack_base + offset; + pack = (unsigned char *) p->pack_base + offset; c = *pack++; offset++; *type = (c >>4 ) &7 ;
@@ -883,7 +884,7 @@ int check_reuse_pack_delta(struct packed ptr =unpack_object_header (p, ptr, kindp, sizep);if (*kindp !=OBJ_DELTA ) goto done; -memcpy (base, p->pack_base + ptr,20 ); +memcpy (base, (char *) p->pack_base + ptr,20 ); status =0 ; done:unuse_packed_git (p);
@@ -903,7 +904,7 @@ void packed_object_info_detail(struct paenum object_type kind; offset =unpack_object_header (p, e->offset, &kind, size); - pack = p->pack_base + offset; + pack = (unsigned char *) p->pack_base + offset;if (kind !=OBJ_DELTA ) *delta_chain_length =0 ;else {
@@ -919,7 +920,7 @@ void packed_object_info_detail(struct pafind_pack_entry_one (pack, &base_ent, p); offset =unpack_object_header (p, base_ent.offset, &kind, &junk); - pack = p->pack_base + offset; + pack = (unsigned char *) p->pack_base + offset; chain_length++; }while (kind ==OBJ_DELTA ); *delta_chain_length = chain_length;
@@ -957,7 +958,7 @@ static int packed_object_info(struct pacdie ("cannot map packed file" ); offset =unpack_object_header (p, entry->offset, &kind, &size); - pack = p->pack_base + offset; + pack = (unsigned char *) p->pack_base + offset; left = p->pack_size - offset;switch (kind) {
@@ -1096,7 +1097,7 @@ void *unpack_entry_gently(struct pack_envoid *retval; offset =unpack_object_header (p, entry->offset, &kind, &size); - pack = p->pack_base + offset; + pack = (unsigned char *) p->pack_base + offset; left = p->pack_size - offset;switch (kind) {case OBJ_DELTA :
@@ -1134,7 +1135,7 @@ int nth_packed_object_sha1(const structvoid *index = p->index_base +256 ;if (n <0 ||num_packed_objects (p) <= n)return -1 ; -memcpy (sha1, (index +24 * n +4 ),20 ); +memcpy (sha1, (char *) index + (24 * n) +4 ,20 );return 0 ; }
@@ -1148,9 +1149,9 @@ int find_pack_entry_one(const unsigned c do { int mi = (lo + hi) / 2; - int cmp = memcmp(index + 24 * mi + 4, sha1, 20); + int cmp = memcmp((char *) index + (24 * mi) + 4, sha1, 20); if (!cmp) { - e->offset = ntohl(*((unsigned int *)(index + 24 * mi))); + e->offset = ntohl(*((unsigned int *) ((char *) index + (24 * mi)))); memcpy(e->sha1, sha1, 20); e->p = p; return 1;
@@ -1290,7 +1291,7 @@ void *read_object_with_reference(const u ref_length =strlen (ref_type);if (memcmp (buffer, ref_type, ref_length) || -get_sha1_hex (buffer + ref_length, actual_sha1)) { +get_sha1_hex ((char *) buffer + ref_length, actual_sha1)) {free (buffer);return NULL ; }
@@ -1408,7 +1409,7 @@ static int write_buffer(int fd, const voreturn error ("file write error (%s)" ,strerror (errno)); } len -= size; - buf += size; + buf = (char *) buf + size; }return 0 ; }
ssh-fetch.c +1 -1
@@ -24,7 +24,7 @@ static ssize_t force_write(int fd, void {ssize_t ret =0 ;while (ret < length) { -ssize_t size =write (fd, buffer + ret, length - ret); +ssize_t size =write (fd, (char *) buffer + ret, length - ret);if (size <0 ) {return size; }
tag.c +2 -2
@@ -47,10 +47,10 @@ int parse_tag_buffer(struct tag *item, vif (size <64 )return -1 ; -if (memcmp ("object " , data,7 ) ||get_sha1_hex (data +7 , object)) +if (memcmp ("object " , data,7 ) ||get_sha1_hex ((char *) data +7 , object))return -1 ; - type_line = data +48 ; + type_line = (char *) data +48 ;if (memcmp ("\ntype " , type_line-1 ,6 ))return -1 ;
tree-walk.c +6 -5
@@ -43,7 +43,7 @@ void update_tree_entry(struct tree_descif (size < len)die ("corrupt tree file" ); - desc->buf = buf + len; + desc->buf = (char *) buf + len; desc->size = size - len; }
@@ -66,7 +66,7 @@ const unsigned char *tree_entry_extract(const void *tree = desc->buf;unsigned long size = desc->size;int len =strlen (tree)+1 ; -const unsigned char *sha1 = tree + len; +const unsigned char *sha1 = (unsigned char *) tree + len;const char *path;unsigned int mode;
@@ -80,7 +80,8 @@ const unsigned char *tree_entry_extract(int tree_entry (struct tree_desc *desc,struct name_entry *entry) { -const void *tree = desc->buf, *path; +const void *tree = desc->buf; +const char *path;unsigned long len, size = desc->size;if (!size)
@@ -95,10 +96,10 @@ int tree_entry(struct tree_desc *desc, s entry->pathlen = len; path += len +1 ; - entry->sha1 = path; + entry->sha1 = (const unsigned char *) path; path +=20 ; - len = path - tree; + len = path - (char *) tree;if (len > size)die ("corrupt tree file" );
Signature · 1 line
1.3.3
Quoted 7 lines · Florian Forster
Using this patch I was able to build git with
$ make CFLAGS="-Wall -Werror -ansi -pedantic -std=c99 -D_XOPEN_SOURCE=500 -D_BSD_SOURCE"While most of this patch fixes void-pointer arithmetic and is therefore trivial, I had to change the use of a struct with FAMs in `diff-lib.c'. Since this is the first time I encountered FAMs it'd probably be a good idea if someone who knows would take a look at that.
Many of the void-pointer arithmetic warnings could be fixed by changing the variable types to char * instead of casting them in many places.
diff-lib.c int run_diff_files(struct rev_info *revs · line 40
Quoted 10 lines
diff-lib.c +0
-4@@ -34,21 +34,23 @@ int run_diff_files(struct rev_info *revscontinue ;if (ce_stage (ce)) { -struct { -struct combine_diff_path p; -struct combine_diff_parent filler[5 ]; - } combine;
Yes this is somewhat ugly but avoids a malloc. You could use alloca but the man-page says:
"The alloca() function is machine and compiler dependent. On many systems
its implementation is buggy. Its use is discouraged."
Leave the code as it is now unless it causes real problems.
diff-lib.c int run_diff_files(struct rev_info *revs · line 44
Quoted 11 lines
diff-lib.c +6
-3static inline int needs_quote (int ch) { -switch (ch) { -case '/' :case '-' :case '.' : - case 'A'...'Z': case 'a'...'z': case '0'...'9': +if (((ch >='A' ) && (ch <='Z' )) + || ((ch >='a' ) && (ch <='z' )) + || ((ch >='0' ) && (ch <='9' )) + || (ch =='/' ) + || (ch =='-' ) + || (ch =='.' ))
'A'...'Z' is more readable. Does some compiler fail to compile it?
diff-lib.c int run_diff_files(struct rev_info *revs · line 47
Quoted 3 lines
diff-lib.c +1
-0+++ b/http-push.cstatic inline int needs_quote (int ch)
Hmm.. same function in http-fetch.c. Lots of common code could be moved to http.h.
Signature · 1 line
http://onion.dynserv.net/~timo/
Hello,
Quoted 1 line
'A'...'Z' is more readable. Does some compiler fail to compile it?
yes, it is GCC (GNU) specific IIRC. So for example the Sun Compiler will choke on this one.
Thomas
Hi,
Quoted 3 lines · Timo Hirvonen
Many of the void-pointer arithmetic warnings could be fixed by changing the variable types to char * instead of casting them in many places.
true. Casting to a void-pointer can be done implicitely (even according to the standard), so changing a function's argument from void *' to char *' might break code somewhere else.
Quoted 6 lines
diff-lib.c int run_diff_files(struct rev_info *revs · line 40
Quoted 4 lines
diff-lib.c +0
-4-struct { -struct combine_diff_path p; -struct combine_diff_parent filler[5 ]; - } combine;Yes this is somewhat ugly but avoids a malloc.
No, malloc is still needed to allocate `combine.p.path'. My change does allocate all memory in one pass, so the number of calls to malloc doesn't differ.
Quoted 1 line
Leave the code as it is now unless it causes real problems.
`struct combine_diff_path' has a flexible array member (FAM) and is therefore an incomplete type. This means you may not instanciate it.
GCC ignores the FAM in this case and allocates `sizeof (struct combine_diff_path)' bytes. However, this is not correct according to ANSI and prevents building using other compilers (e.g. Sun cc).
To be honest, I don't get the point of FAMs anyways. Why don't we just use a pointer to `struct combine_diff_parent' there in the first place?
Quoted 1 line
'A'...'Z' is more readable. Does some compiler fail to compile it?
It's not in ANSI, therefore GCC with `-ansi' failes, so does the Sun cc.
Quoted 4 lines
Quoted 1 line
static inline int
needs_quote(int ch)Hmm.. same function in
http-fetch.c. Lots of common code could be moved tohttp.h.
Right, I wanted to keep the changes as minimal as possible, though.
As you can probably tell my goal is building a Solaris version of git using the Sun cc. I'm not quite there yet and don't have time to look into the remaining issue(s) today nor tomorrow, but I might have some time at the end of the week.
Regards,
-octo
Signature · 4 lines
Florian octo Forster Hacker in training GnuPG: 0x91523C3D http://verplant.org/
Quoted 3 lines · Florian Forster
GCC ignores the FAM in this case and allocates `sizeof (struct
combine_diff_path)' bytes. However, this is not correct according to ANSI and prevents building using other compilers (e.g. Sun cc).
Fair enough.
Quoted 2 lines
To be honest, I don't get the point of FAMs anyways. Why don't we just use a pointer to `struct
combine_diff_parent' there in the first place?
In general FAMs are used to replace two mallocs with one.
x = malloc(sizeof(struct foo) + 100)
instead of
x = malloc(sizeof(struct foo));
x->y = malloc(100);Signature · 1 line
http://onion.dynserv.net/~timo/
Quoted 7 lines · Florian Forster
Using this patch I was able to build git with
$ make CFLAGS="-Wall -Werror -ansi -pedantic -std=c99 -D_XOPEN_SOURCE=500 -D_BSD_SOURCE"While most of this patch fixes void-pointer arithmetic and is therefore trivial, I had to change the use of a struct with FAMs in `diff-lib.c'. Since this is the first time I encountered FAMs it'd probably be a good idea if someone who knows would take a look at that.
Nice work! However, could you split it up a bit? I.e. please send one patch per error class/topic. A comment on why the changes are needed would be nice too, e.g. "void pointer arithmetic is a GNU extension, get rid of it".
Thanks,
René
Hi Rene,
Quoted 1 line · Rene Scharfe
However, could you split it up a bit?
Sure, I'll follow up on this in a short while..
-octo
Signature · 4 lines
Florian octo Forster Hacker in training GnuPG: 0x91523C3D http://verplant.org/
cover Improve ANSI C99 compliance
Hi,
as promised I've split up the changes into several smaller patches. The changes are grouped by type of problem they fix, as requested. The patches should not depend on each other, but I didn't actually test that.
Regards,
-octo
PATCH 1/7 Remove ranges from switch statements.
Though very nice and readable, the "case 'a'...'z':" construct is not ANSI C99 compliant. This patch unfolds the range in quote.c' and substitutes the switch-statement with an if-statement in http-fetch.c' and `http-push.c'.
---
http-fetch.c | 13 +++++++------ http-push.c | 13 +++++++------ quote.c | 9 ++++++++- 3 files changed, 22 insertions(+), 13 deletions(-)
d90149c5b4e91938329120bdde609e5f6d9b03e8
http-fetch.c +7 -6
@@ -1136,13 +1136,14 @@ int fetch(unsigned char *sha1)static inline int needs_quote (int ch) { -switch (ch) { -case '/' :case '-' :case '.' : - case 'A'...'Z': case 'a'...'z': case '0'...'9': +if (((ch >='A' ) && (ch <='Z' )) + || ((ch >='a' ) && (ch <='z' )) + || ((ch >='0' ) && (ch <='9' )) + || (ch =='/' ) + || (ch =='-' ) + || (ch =='.' ))return 0 ; -default : -return 1 ; - } +return 1 ; }static inline int hex (int v)
http-push.c +7 -6
@@ -1077,13 +1077,14 @@ static int fetch_indices(void)static inline int needs_quote (int ch) { -switch (ch) { -case '/' :case '-' :case '.' : - case 'A'...'Z': case 'a'...'z': case '0'...'9': +if (((ch >='A' ) && (ch <='Z' )) + || ((ch >='a' ) && (ch <='z' )) + || ((ch >='0' ) && (ch <='9' )) + || (ch =='/' ) + || (ch =='-' ) + || (ch =='.' ))return 0 ; -default : -return 1 ; - } +return 1 ; }static inline int hex (int v)
quote.c +8 -1
@@ -206,7 +206,14 @@ #define EMIT(c) (outp ? (*outp++ = (c))case '\\' :case '"' :break ;/* verbatim */ - case '0'...'7': +case '0' : +case '1' : +case '2' : +case '3' : +case '4' : +case '5' : +case '6' : +case '7' :/* octal */ ac = ((ch -'0' ) <<6 );if ((ch = *sp++) <'0' ||'7' < ch)
Signature · 1 line
1.3.3
PATCH 2/7 Initialize FAMs using `FLEX_ARRAY'.
When initializing a flexible array member' the macro FLEX_ARRAY' should be used. This was forgotten in `diff-delta.c'.
---
diff-delta.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
e587fd59510f2ed8326d280054d78cfb78d482dc
diff-delta.c +2 -1
@@ -22,6 +22,7 @@ #include <stdlib.h>#include <string.h> #include "delta.h" +#include "git-compat-util.h" /* maximum hash entry list for the same hash bucket */ #define HASH_LIMIT 64
@@ -131,7 +132,7 @@ struct delta_index {const void *src_buf;unsigned long src_size;unsigned int hash_mask; -struct index_entry *hash[0 ]; +struct index_entry *hash[FLEX_ARRAY ]; };struct delta_index *create_delta_index (const void *buf,unsigned long bufsize)
Signature · 1 line
1.3.3
PATCH 3/7 Don't instantiate structures with FAMs.
Since structures with flexible array members' are an incomplete datatype ANSI C99 forbids creating instances of them. This patch removes such an instance from diff-lib.c' and replaces it with a pointer to a struct combine_diff_path'. Since all neccessary memory is allocated at once the number of calls to xmalloc' is not increased.
---
diff-lib.c | 41 ++++++++++++++++++++++------------------- 1 files changed, 22 insertions(+), 19 deletions(-)
c163a36f0bd0e07ffb9ee7d4bfb22f1cbb38eef8
diff-lib.c +22 -19
@@ -34,21 +34,23 @@ int run_diff_files(struct rev_info *revscontinue ;if (ce_stage (ce)) { -struct { -struct combine_diff_path p; -struct combine_diff_parent filler[5 ]; - } combine; +struct combine_diff_path *dpath;int num_compare_stages =0 ; +size_t path_len; - combine.p.next =NULL ; - combine.p.len =ce_namelen (ce); - combine.p.path =xmalloc (combine.p.len +1 ); -memcpy (combine.p.path, ce->name, combine.p.len); - combine.p.path[combine.p.len] =0 ; - combine.p.mode =0 ; -memset (combine.p.sha1,0 ,20 ); -memset (&combine.p.parent[0 ],0 , -sizeof (combine.filler)); + path_len =ce_namelen (ce); + + dpath =xmalloc (combine_diff_path_size (5 , path_len)); + dpath->path = (char *) &(dpath->parent[5 ]); + + dpath->next =NULL ; + dpath->len = path_len; +memcpy (dpath->path, ce->name, path_len); + dpath->path[path_len] ='\0' ; + dpath->mode =0 ; +memset (dpath->sha1,0 ,20 ); +memset (&(dpath->parent[0 ]),0 , +sizeof (struct combine_diff_parent )*5 );while (i < entries) {struct cache_entry *nce = active_cache[i];
@@ -64,11 +66,11 @@ int run_diff_files(struct rev_info *revsif (2 <= stage) {int mode =ntohl (nce->ce_mode); num_compare_stages++; -memcpy (combine.p.parent[stage-2 ].sha1, +memcpy (dpath->parent[stage-2 ].sha1, nce->sha1,20 ); - combine.p.parent[stage-2 ].mode = + dpath->parent[stage-2 ].mode =canon_mode (mode); - combine.p.parent[stage-2 ].status = + dpath->parent[stage-2 ].status =DIFF_STATUS_MODIFIED ; }
@@ -83,13 +85,14 @@ int run_diff_files(struct rev_info *revs i--;if (revs->combine_merges && num_compare_stages ==2 ) { -show_combined_diff (&combine.p,2 , +show_combined_diff (dpath,2 , revs->dense_combined_merges, revs); -free (combine.p.path); +free (dpath);continue ; } -free (combine.p.path); +free (dpath); + dpath =NULL ;/* * Show the diff for the 'ce' if we found the one
Signature · 1 line
1.3.3
PATCH 4/7 Cast pointers to `void *' when used in a format.
ANSI C99 requires void-pointers when using the %p' format. This patch adds the neccessary cast in blame.c'.
---
blame.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
372bb52dd567d39c3e64919e100ae9bd8af603ca
blame.c +2 -2
@@ -301,9 +301,9 @@ static void fill_line_map(struct commitif (DEBUG )printf ("map: i1: %d %d %p i2: %d %d %p\n" , i1, map[i1], - i1 !=-1 ? blame_lines[map[i1]] :NULL , + (void *) (i1 !=-1 ? blame_lines[map[i1]] :NULL ), i2, map2[i2], - i2 !=-1 ? blame_lines[map2[i2]] :NULL ); + (void *) (i2 !=-1 ? blame_lines[map2[i2]] :NULL ));if (map2[i2] !=-1 && blame_lines[map[i1]] && !blame_lines[map2[i2]])
Signature · 1 line
1.3.3
PATCH 5/7 Don't use empty structure initializers.
Empty initializers for structures are not allowed in ANSI C99. This patch removes such an initializer from `builtin-read-tree.c'. Since the struct was static (and is therefore implicitely initialized to zero anyway) it wasn't actually needed.
---
builtin-read-tree.c | 3 +-- 1 files changed, 1 insertions(+), 2 deletions(-)
91df4330d35f1f1670dd04af0a14f1ca84a72b2b
builtin-read-tree.c +1 -2
@@ -30,8 +30,7 @@ static int merge_size = 0;static struct object_list *trees =NULL ; -static struct cache_entry df_conflict_entry = { -}; +static struct cache_entry df_conflict_entry;struct tree_entry_list {struct tree_entry_list *next;
Signature · 1 line
1.3.3
PATCH 6/7 Change types used in bitfields to be `int's.
According to ANSI C99 bitfields are only defined for signed int' and unsigned int'. This patch corrects the bitfield in the msg_data_t' type from imap-send.c'.
---
imap-send.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
471838c5e32b83bc8b84584343daeb1174d0e968
imap-send.c +1 -1
@@ -93,7 +93,7 @@ typedef struct {char *data;int len;unsigned char flags; - unsigned char crlf:1; + unsigned int crlf:1; } msg_data_t;#define DRV_OK 0
Signature · 1 line
1.3.3
PATCH 7/7 Remove all void-pointer arithmetic.
ANSI C99 doesn't allow void-pointer arithmetic. This patch fixes this in various ways. Usually the strategy that required the least changes was used.
---
builtin-apply.c | 6 +++--- builtin-tar-tree.c | 6 +++--- convert-objects.c | 22 +++++++++++----------- csum-file.c | 4 ++-- diff-delta.c | 2 +- diff.c | 2 +- diffcore-order.c | 2 +- http-fetch.c | 2 +- http-push.c | 2 +- http.c | 4 ++-- pack-check.c | 6 +++--- pack-objects.c | 4 ++-- pack-redundant.c | 16 ++++++++-------- patch-delta.c | 4 ++-- pkt-line.c | 4 ++-- read-cache.c | 13 +++++++------ sha1_file.c | 27 ++++++++++++++------------- ssh-fetch.c | 2 +- tag.c | 4 ++-- tree-walk.c | 11 ++++++----- 20 files changed, 73 insertions(+), 70 deletions(-)
13941d158a5f53456cc095b1a4893c177c7224c0
builtin-apply.c +3 -3
@@ -148,7 +148,7 @@ static void *read_patch_file(int fd, uns buffer =xrealloc (buffer, alloc); nr = alloc - size; } - nr =xread (fd, buffer + size, nr); + nr =xread (fd, (char *) buffer + size, nr);if (!nr)break ;if (nr <0 )
@@ -164,7 +164,7 @@ static void *read_patch_file(int fd, uns*/ if (alloc < size +SLOP ) buffer =xrealloc (buffer, size +SLOP ); -memset (buffer + size,0 ,SLOP ); +memset ((char *) buffer + size,0 ,SLOP );return buffer; }
@@ -1194,7 +1194,7 @@ static int read_old_data(struct stat *streturn error ("unable to open %s" , path); got =0 ;for (;;) { -int ret =xread (fd, buf + got, size - got); +int ret =xread (fd, (char *) buf + got, size - got);if (ret <=0 )break ; got += ret;
builtin-tar-tree.c +3 -3
@@ -34,7 +34,7 @@ static void reliable_write(void *buf, undie ("git-tar-tree: disk full?" ); } size -= ret; - buf += ret; + buf = (char *) buf + ret; } }
@@ -87,13 +87,13 @@ static void write_blocked(void *buf, unsmemcpy (block + offset, buf, chunk); size -= chunk; offset += chunk; - buf += chunk; + buf = (char *) buf + chunk;write_if_needed (); }while (size >=BLOCKSIZE ) {reliable_write (buf,BLOCKSIZE ); size -=BLOCKSIZE ; - buf +=BLOCKSIZE ; + buf = (char *) buf +BLOCKSIZE ; }if (size) {memcpy (block + offset, buf, size);
convert-objects.c +11 -11
@@ -103,12 +103,12 @@ static int write_subdirectory(void *buffif (!slash) { newlen +=sprintf (new + newlen,"%o %s" , mode, path); new[newlen++] ='\0' ; -memcpy (new + newlen, buffer + len -20 ,20 ); +memcpy (new + newlen, (char *) buffer + len -20 ,20 ); newlen +=20 ; used += len; size -= len; - buffer += len; + buffer = (char *) buffer + len;continue ; }
@@ -121,7 +121,7 @@ static int write_subdirectory(void *buff used += len; size -= len; - buffer += len; + buffer = (char *) buffer + len; }write_sha1_file (new, newlen, tree_type, result_sha1);
@@ -137,13 +137,13 @@ static void convert_tree(void *buffer, uwhile (size) {int len =1 +strlen (buffer); -convert_binary_sha1 (buffer + len); +convert_binary_sha1 ((char *) buffer + len); len +=20 ;if (len > size)die ("corrupt tree object" ); size -= len; - buffer += len; + buffer = (char *) buffer + len; }write_subdirectory (orig_buffer, orig_size,"" ,0 , result_sha1);
@@ -244,14 +244,14 @@ static void convert_date(void *buffer, u// "tree <sha1>\n" memcpy (new + newlen, buffer,46 ); newlen +=46 ; - buffer +=46 ; + buffer = (char *) buffer +46 ; size -=46 ;// "parent <sha1>\n" while (!memcmp (buffer,"parent " ,7 )) {memcpy (new + newlen, buffer,48 ); newlen +=48 ; - buffer +=48 ; + buffer = (char *) buffer +48 ; size -=48 ; }
@@ -275,11 +275,11 @@ static void convert_commit(void *buffer,if (memcmp (buffer,"tree " ,5 ))die ("Bad commit '%s'" , (char *) buffer); -convert_ascii_sha1 (buffer+5 ); - buffer +=46 ;/* "tree " + "hex sha1" + "\n" */ +convert_ascii_sha1 ((char *) buffer +5 ); + buffer = (char *) buffer +46 ;/* "tree " + "hex sha1" + "\n" */ while (!memcmp (buffer,"parent " ,7 )) { -convert_ascii_sha1 (buffer+7 ); - buffer +=48 ; +convert_ascii_sha1 ((char *) buffer +7 ); + buffer = (char *) buffer +48 ; }convert_date (orig_buffer, orig_size, result_sha1); }
csum-file.c +2 -2
@@ -17,7 +17,7 @@ static int sha1flush(struct sha1file *f,for (;;) {int ret =xwrite (f->fd, buf, count);if (ret >0 ) { - buf += ret; + buf = (char *) buf + ret; count -= ret;if (count)continue ;
@@ -57,7 +57,7 @@ int sha1write(struct sha1file *f, void *memcpy (f->buffer + offset, buf, nr); count -= nr; offset += nr; - buf += nr; + buf = (char *) buf + nr; left -= nr;if (!left) {SHA1_Update (&f->ctx, f->buffer, offset);
diff-delta.c +1 -1
@@ -284,7 +284,7 @@ create_delta(const struct delta_index *i ref_data = index->src_buf; ref_top = ref_data + index->src_size; data = trg_buf; - top = trg_buf + trg_size; + top = (const unsigned char *) trg_buf + trg_size; outpos++; val =0 ;
diff.c +1 -1
@@ -515,7 +515,7 @@ static void emit_binary_diff(mmfile_t *oelse line[0 ] = bytes -26 +'a' -1 ;encode_85 (line +1 , cp, bytes); - cp += bytes; + cp = (char *) cp + bytes;puts (line); }printf ("\n" );
diffcore-order.c +1 -1
@@ -30,7 +30,7 @@ static void prepare_order(const char *orclose (fd);if (map ==MAP_FAILED )return ; - endp = map + st.st_size; + endp = (char *) map + st.st_size;for (pass =0 ; pass <2 ; pass++) { cnt =0 ; cp = map;
http-fetch.c +1 -1
@@ -123,7 +123,7 @@ static size_t fwrite_sha1_file(void *ptrstruct object_request *obj_req = (struct object_request *)data; do { ssize_t retval = write(obj_req->local, - ptr + posn, size - posn); + (char *) ptr + posn, size - posn); if (retval < 0) return posn; posn += retval;
http-push.c +1 -1
@@ -196,7 +196,7 @@ static size_t fwrite_sha1_file(void *ptrstruct transfer_request *request = (struct transfer_request *)data; do { ssize_t retval = write(request->local_fileno, - ptr + posn, size - posn); + (char *) ptr + posn, size - posn); if (retval < 0) return posn; posn += retval;
http.c +2 -2
@@ -34,7 +34,7 @@ size_t fread_buffer(void *ptr, size_t elsize_t size = eltsize * nmemb;if (size > buffer->size - buffer->posn) size = buffer->size - buffer->posn; -memcpy (ptr, buffer->buffer + buffer->posn, size); +memcpy (ptr, (char *) buffer->buffer + buffer->posn, size); buffer->posn += size;return size; }
@@ -49,7 +49,7 @@ size_t fwrite_buffer(const void *ptr, si buffer->size = buffer->posn + size; buffer->buffer =xrealloc (buffer->buffer, buffer->size); } -memcpy (buffer->buffer + buffer->posn, ptr, size); +memcpy ((char *) buffer->buffer + buffer->posn, ptr, size); buffer->posn += size; data_received++;return size;
pack-check.c +3 -3
@@ -29,10 +29,10 @@ static int verify_packfile(struct packed pack_base = p->pack_base;SHA1_Update (&ctx, pack_base, pack_size -20 );SHA1_Final (sha1, &ctx); -if (memcmp (sha1, pack_base + pack_size -20 ,20 )) +if (memcmp (sha1, (char *) pack_base + pack_size -20 ,20 ))return error ("Packfile %s SHA1 mismatch with itself" , p->pack_name); -if (memcmp (sha1, index_base + index_size -40 ,20 )) +if (memcmp (sha1, (char *) index_base + index_size -40 ,20 ))return error ("Packfile %s SHA1 mismatch with idx" , p->pack_name);
@@ -135,7 +135,7 @@ int verify_pack(struct packed_git *p, inSHA1_Init (&ctx);SHA1_Update (&ctx, index_base, index_size -20 );SHA1_Final (sha1, &ctx); -if (memcmp (sha1, index_base + index_size -20 ,20 )) +if (memcmp (sha1, (char *) index_base + index_size -20 ,20 )) ret =error ("Packfile index for %s SHA1 mismatch" , p->pack_name);
pack-objects.c +2 -2
@@ -156,7 +156,7 @@ static void prepare_pack_revindex(struct rix->revindex =xmalloc (sizeof (unsigned long ) * (num_ent +1 ));for (i =0 ; i < num_ent; i++) { -unsigned int hl = *((unsigned int *)(index +24 * i)); +unsigned int hl = *((unsigned int *)((char *) index +24 *i)); rix->revindex[i] =ntohl (hl); }/* This knows the pack format -- the 20-byte trailer
@@ -300,7 +300,7 @@ static unsigned long write_object(structuse_packed_git (p); datalen =find_packed_object_size (p, entry->in_pack_offset); - buf = p->pack_base + entry->in_pack_offset; + buf = (char *) p->pack_base + entry->in_pack_offset;sha1write (f, buf, datalen);unuse_packed_git (p); hdrlen =0 ;/* not really */
pack-redundant.c +8 -8
@@ -246,12 +246,12 @@ static struct pack_list * pack_list_diffstatic void cmp_two_packs (struct pack_list *p1,struct pack_list *p2) {int p1_off, p2_off; -void *p1_base, *p2_base; +unsigned char *p1_base, *p2_base;struct llist_item *p1_hint =NULL , *p2_hint =NULL ; p1_off = p2_off =256 *4 +4 ; - p1_base = (void *)p1->pack->index_base; - p2_base = (void *)p2->pack->index_base; + p1_base = (unsigned char *) p1->pack->index_base; + p2_base = (unsigned char *) p2->pack->index_base;while (p1_off <= p1->pack->index_size -3 *20 && p2_off <= p2->pack->index_size -3 *20 )
@@ -351,11 +351,11 @@ static size_t sizeof_union(struct packed {size_t ret =0 ;int p1_off, p2_off; -void *p1_base, *p2_base; +char *p1_base, *p2_base; p1_off = p2_off =256 *4 +4 ; - p1_base = (void *)p1->index_base; - p2_base = (void *)p2->index_base; + p1_base = (char *)p1->index_base; + p2_base = (char *)p2->index_base;while (p1_off <= p1->index_size -3 *20 && p2_off <= p2->index_size -3 *20 )
@@ -534,7 +534,7 @@ static struct pack_list * add_pack(struc {struct pack_list l;size_t off; -void *base; +unsigned char *base;if (!p->pack_local && !(alt_odb || verbose))return NULL ;
@@ -543,7 +543,7 @@ static struct pack_list * add_pack(strucllist_init (&l.all_objects); off =256 *4 +4 ; - base = (void *)p->index_base; + base = (unsigned char *)p->index_base;while (off <= p->index_size -3 *20 ) {llist_insert_back (l.all_objects, base + off); off +=24 ;
patch-delta.c +2 -2
@@ -25,7 +25,7 @@ void *patch_delta(const void *src_buf, ureturn NULL ; data = delta_buf; - top = delta_buf + delta_size; + top = (const unsigned char *) delta_buf + delta_size;/* make sure the orig file size matches what we expect */ size =get_delta_hdr_size (&data, top);
@@ -56,7 +56,7 @@ void *patch_delta(const void *src_buf, u cp_off + cp_size > src_size || cp_size > size) goto bad; -memcpy (out, src_buf + cp_off, cp_size); +memcpy (out, (char *) src_buf + cp_off, cp_size); out += cp_size; size -= cp_size; }else if (cmd) {
pkt-line.c +2 -2
@@ -21,7 +21,7 @@ static void safe_write(int fd, const voiwhile (n) {int ret =xwrite (fd, buf, n);if (ret >0 ) { - buf += ret; + buf = (char *) buf + ret; n -= ret;continue ; }
@@ -66,7 +66,7 @@ static void safe_read(int fd, void *buffint n =0 ;while (n < size) { -int ret =xread (fd, buffer + n, size - n); +int ret =xread (fd, (char *) buffer + n, size - n);if (ret <0 )die ("read error (%s)" ,strerror (errno));if (!ret)
read-cache.c +7 -6
@@ -706,7 +706,7 @@ static int verify_hdr(struct cache_headeSHA1_Init (&c);SHA1_Update (&c, hdr, size -20 );SHA1_Final (sha1, &c); -if (memcmp (sha1, (void *)hdr + size -20 ,20 )) +if (memcmp (sha1, (char *) hdr + size -20 ,20 ))return error ("bad index file sha1 signature" );return 0 ; }
@@ -770,7 +770,7 @@ int read_cache(void) offset =sizeof (*hdr);for (i =0 ; i < active_nr; i++) { -struct cache_entry *ce = map + offset; +struct cache_entry *ce = (struct cache_entry *) ((char *) map + offset); offset = offset +ce_size (ce); active_cache[i] = ce; }
@@ -783,10 +783,11 @@ int read_cache(void)* in 4-byte network byte order. */ unsigned long extsize; -memcpy (&extsize, map + offset +4 ,4 ); +memcpy (&extsize, (char *) map + offset +4 ,4 ); extsize =ntohl (extsize); -if (read_index_extension (map + offset, - map + offset +8 , extsize) <0 ) +if (read_index_extension (((const char *) map) + offset, + (char *) map + offset +8 , + extsize) <0 ) goto unmap; offset +=8 ; offset += extsize;
@@ -820,7 +821,7 @@ static int ce_write(SHA_CTX *context, in } write_buffer_len = buffered; len -= partial; - data += partial; + data = (char *) data + partial; }return 0 ; }
sha1_file.c +14 -13
@@ -486,7 +486,8 @@ int use_packed_git(struct packed_git *p)* this is cheap. */ if (memcmp ((char *)(p->index_base) + p->index_size -40 , - p->pack_base + p->pack_size -20 ,20 )) { + (char *) p->pack_base + p->pack_size -20 , +20 )) {die ("packfile %s does not match index." , p->pack_name); }
@@ -701,7 +702,7 @@ static void *unpack_sha1_rest(z_stream *int bytes =strlen (buffer) +1 ;unsigned char *buf =xmalloc (1 +size); -memcpy (buf, buffer + bytes, stream->total_out - bytes); +memcpy (buf, (char *) buffer + bytes, stream->total_out - bytes); bytes = stream->total_out - bytes;if (bytes < size) { stream->next_out = buf + bytes;
@@ -853,7 +854,7 @@ static unsigned long unpack_object_headeif (offset >= p->pack_size)die ("object offset outside of pack file" ); - pack = p->pack_base + offset; + pack = (unsigned char *) p->pack_base + offset; c = *pack++; offset++; *type = (c >>4 ) &7 ;
@@ -883,7 +884,7 @@ int check_reuse_pack_delta(struct packed ptr =unpack_object_header (p, ptr, kindp, sizep);if (*kindp !=OBJ_DELTA ) goto done; -memcpy (base, p->pack_base + ptr,20 ); +memcpy (base, (char *) p->pack_base + ptr,20 ); status =0 ; done:unuse_packed_git (p);
@@ -903,7 +904,7 @@ void packed_object_info_detail(struct paenum object_type kind; offset =unpack_object_header (p, e->offset, &kind, size); - pack = p->pack_base + offset; + pack = (unsigned char *) p->pack_base + offset;if (kind !=OBJ_DELTA ) *delta_chain_length =0 ;else {
@@ -919,7 +920,7 @@ void packed_object_info_detail(struct pafind_pack_entry_one (pack, &base_ent, p); offset =unpack_object_header (p, base_ent.offset, &kind, &junk); - pack = p->pack_base + offset; + pack = (unsigned char *) p->pack_base + offset; chain_length++; }while (kind ==OBJ_DELTA ); *delta_chain_length = chain_length;
@@ -957,7 +958,7 @@ static int packed_object_info(struct pacdie ("cannot map packed file" ); offset =unpack_object_header (p, entry->offset, &kind, &size); - pack = p->pack_base + offset; + pack = (unsigned char *) p->pack_base + offset; left = p->pack_size - offset;switch (kind) {
@@ -1096,7 +1097,7 @@ void *unpack_entry_gently(struct pack_envoid *retval; offset =unpack_object_header (p, entry->offset, &kind, &size); - pack = p->pack_base + offset; + pack = (unsigned char *) p->pack_base + offset; left = p->pack_size - offset;switch (kind) {case OBJ_DELTA :
@@ -1134,7 +1135,7 @@ int nth_packed_object_sha1(const structvoid *index = p->index_base +256 ;if (n <0 ||num_packed_objects (p) <= n)return -1 ; -memcpy (sha1, (index +24 * n +4 ),20 ); +memcpy (sha1, (char *) index + (24 * n) +4 ,20 );return 0 ; }
@@ -1148,9 +1149,9 @@ int find_pack_entry_one(const unsigned c do { int mi = (lo + hi) / 2; - int cmp = memcmp(index + 24 * mi + 4, sha1, 20); + int cmp = memcmp((char *) index + (24 * mi) + 4, sha1, 20); if (!cmp) { - e->offset = ntohl(*((unsigned int *)(index + 24 * mi))); + e->offset = ntohl(*((unsigned int *) ((char *) index + (24 * mi)))); memcpy(e->sha1, sha1, 20); e->p = p; return 1;
@@ -1290,7 +1291,7 @@ void *read_object_with_reference(const u ref_length =strlen (ref_type);if (memcmp (buffer, ref_type, ref_length) || -get_sha1_hex (buffer + ref_length, actual_sha1)) { +get_sha1_hex ((char *) buffer + ref_length, actual_sha1)) {free (buffer);return NULL ; }
@@ -1408,7 +1409,7 @@ static int write_buffer(int fd, const voreturn error ("file write error (%s)" ,strerror (errno)); } len -= size; - buf += size; + buf = (char *) buf + size; }return 0 ; }
ssh-fetch.c +1 -1
@@ -24,7 +24,7 @@ static ssize_t force_write(int fd, void {ssize_t ret =0 ;while (ret < length) { -ssize_t size =write (fd, buffer + ret, length - ret); +ssize_t size =write (fd, (char *) buffer + ret, length - ret);if (size <0 ) {return size; }
tag.c +2 -2
@@ -47,10 +47,10 @@ int parse_tag_buffer(struct tag *item, vif (size <64 )return -1 ; -if (memcmp ("object " , data,7 ) ||get_sha1_hex (data +7 , object)) +if (memcmp ("object " , data,7 ) ||get_sha1_hex ((char *) data +7 , object))return -1 ; - type_line = data +48 ; + type_line = (char *) data +48 ;if (memcmp ("\ntype " , type_line-1 ,6 ))return -1 ;
tree-walk.c +6 -5
@@ -43,7 +43,7 @@ void update_tree_entry(struct tree_descif (size < len)die ("corrupt tree file" ); - desc->buf = buf + len; + desc->buf = (char *) buf + len; desc->size = size - len; }
@@ -66,7 +66,7 @@ const unsigned char *tree_entry_extract(const void *tree = desc->buf;unsigned long size = desc->size;int len =strlen (tree)+1 ; -const unsigned char *sha1 = tree + len; +const unsigned char *sha1 = (unsigned char *) tree + len;const char *path;unsigned int mode;
@@ -80,7 +80,8 @@ const unsigned char *tree_entry_extract(int tree_entry (struct tree_desc *desc,struct name_entry *entry) { -const void *tree = desc->buf, *path; +const void *tree = desc->buf; +const char *path;unsigned long len, size = desc->size;if (!size)
@@ -95,10 +96,10 @@ int tree_entry(struct tree_desc *desc, s entry->pathlen = len; path += len +1 ; - entry->sha1 = path; + entry->sha1 = (const unsigned char *) path; path +=20 ; - len = path - tree; + len = path - (char *) tree;if (len > size)die ("corrupt tree file" );
Signature · 1 line
1.3.3
Re: [PATCH 1/7] Remove ranges from switch statements.
In reply to Florian Forsterhttp-fetch.c int fetch(unsigned char *sha1)
Quoted 7 lines · Florian Forster
http-fetch.c +4
-3-switch (ch) { -case '/' :case '-' :case '.' : - case 'A'...'Z': case 'a'...'z': case '0'...'9': + if (((ch >= 'A') && (ch <= 'Z')) + || ((ch >= 'a') && (ch <= 'z')) + || ((ch >= '0') && (ch <= '9')) + ...
Sorry for bringing up an old topic again, but wouldn't people agree that this is easier to read if it were written this way ;-)?
if ( (('A' <= ch) && (ch <= 'Z'))
|| (('a' <= ch) && (ch <= 'z'))
|| (('0' <= ch) && (ch <= '9'))
...Re: [PATCH 1/7] Remove ranges from switch statements.
Quoted 7 lines · Junio C Hamano
Sorry for bringing up an old topic again, but wouldn't people agree that this is easier to read if it were written this way ;-)?
if ( (('A' <= ch) && (ch <= 'Z')) || (('a' <= ch) && (ch <= 'z')) || (('0' <= ch) && (ch <= '9')) ...
Yes, but isalnum(ch) even better ;)
Signature · 1 line
http://onion.dynserv.net/~timo/
Quoted 4 lines · Florian Forster
While most of this patch fixes void-pointer arithmetic and is therefore trivial, I had to change the use of a struct with FAMs in `diff-lib.c'. Since this is the first time I encountered FAMs it'd probably be a good idea if someone who knows would take a look at that.
Thanks. I am very tempted to apply it, but I started to wonder that in some places it might make sense to convert void* to char* instead of casting. Undecided.
builtin-tar-tree.c static void reliable_write(void *buf, un · line 37
Quoted 10 lines
builtin-tar-tree.c +1
-1@@ -34,7 +34,7 @@ static void reliable_write(void *buf, undie ("git-tar-tree: disk full?" ); } size -= ret; - buf += ret; + buf = (char *) buf + ret;
Please do not add the extra whitespace to align "=".
Quoted 23 lines
builtin-tar-tree.c +2
-4@@ -244,14 +244,14 @@ static void convert_date(void *buffer, u// "tree <sha1>\n" memcpy (new + newlen, buffer,46 ); newlen +=46 ; - buffer +=46 ; + buffer = (char *) buffer +46 ; size -=46 ;// "parent <sha1>\n" while (!memcmp (buffer,"parent " ,7 )) {memcpy (new + newlen, buffer,48 ); newlen +=48 ; - buffer +=48 ; + buffer = (char *) buffer +48 ; size -=48 ; }@@ -275,11 +275,11 @@ static void convert_commit(void *buffer,if (memcmp (buffer,"tree " ,5 ))die ("Bad commit '%s'" , (char *) buffer); -convert_ascii_sha1 (buffer+5 ); - buffer +=46 ;/* "tree " + "hex sha1" + "\n" */
builtin-tar-tree.c static void convert_commit(void *buffer, · line 285
Quoted 10 lines
builtin-tar-tree.c +4
-2+convert_ascii_sha1 ((char *) buffer +5 ); + buffer = (char *) buffer +46 ;/* "tree " + "hex sha1" + "\n" */ while (!memcmp (buffer,"parent " ,7 )) { -convert_ascii_sha1 (buffer+7 ); - buffer +=48 ; +convert_ascii_sha1 ((char *) buffer +7 ); + buffer = (char *) buffer +48 ; }convert_date (orig_buffer, orig_size, result_sha1); }
Hmmmmmmm. Now I start to wonder if changing the type of "void *buffer" to "char *buffer" is cleaner.
Quoted 13 lines
diff-delta.c +1
-0@@ -22,6 +22,7 @@ #include <stdlib.h>#include <string.h> #include "delta.h" +#include "git-compat-util.h" /* maximum hash entry list for the same hash bucket */ #define HASH_LIMIT 64
diff-delta.c struct delta_index { · line 136
Quoted 6 lines
diff-delta.c +1
-1@@ -131,7 +132,7 @@ struct delta_index {const void *src_buf;unsigned long src_size;unsigned int hash_mask; -struct index_entry *hash[0 ]; +struct index_entry *hash[FLEX_ARRAY ]; };
Good -- I missed this when we did FLEX_ARRAY. Thanks.
diff-lib.c int run_diff_files(struct rev_info *revs · line 40
Quoted 12 lines
diff-lib.c +0
-4@@ -34,21 +34,23 @@ int run_diff_files(struct rev_info *revscontinue ;if (ce_stage (ce)) { -struct { -struct combine_diff_path p; -struct combine_diff_parent filler[5 ]; - } combine;
I admit this part was ugly. The new code does not do any extra allocations and matches the other use of "struct combine_diff_path" more closely. Good change.
Quoted 9 lines
diff-lib.c +2
-3@@ -1136,13 +1136,14 @@ int fetch(unsigned char *sha1)static inline int needs_quote (int ch) { -switch (ch) { -case '/' :case '-' :case '.' : - case 'A'...'Z': case 'a'...'z': case '0'...'9': +if (((ch >='A' ) && (ch <='Z' )) + || ((ch >= 'a') && (ch <= 'z'))
diff-lib.c int fetch(unsigned char *sha1) · line 1147
Quoted 10 lines
diff-lib.c +5
-3+ || ((ch >= '0') && (ch <= '9')) + || (ch == '/') + || (ch == '-') + || (ch == '.'))return 0 ; -default : -return 1 ; - } +return 1 ; }
Ugh. Delight of standard compliance X-<.
http-push.c static int fetch_indices(void) · line 1078
Quoted 7 lines
http-push.c +0
-0@@ -1077,13 +1077,14 @@ static int fetch_indices(void)static inline int needs_quote (int ch)
Hmph. Thanks for noticing the duplicated code; maybe move it to cache.h perhaps?
Quoted 3 lines · Florian Forster
Using this patch I was able to build git with
$ make CFLAGS="-Wall -Werror -ansi -pedantic -std=c99 -D_XOPEN_SOURCE=500 -D_BSD_SOURCE"
"-ansi -pedantic" is really not useful.
Quoted 1 line
While most of this patch fixes void-pointer arithmetic
This one I disagree with. Doing arithmetic on "void *" is _really_ useful, and I think most compilers end up supporting it either to be compatible with gcc, or just because it's hard to not do it.
It makes code a _lot_ cleaner.
In general, explicit casts are a sign of bad programming, and "void *" is there exactly to avoid it. And doing arithmetic on pointers is useful and fairly common, and if you accept void-pointer arithmetic, it avoids a lot of ugly and useless casts.
blame.c static void fill_line_map(struct commit · line 306
Quoted 9 lines
blame.c +2
-2@@ -301,9 +301,9 @@ static void fill_line_map(struct commitif (DEBUG )printf ("map: i1: %d %d %p i2: %d %d %p\n" , i1, map[i1], - i1 !=-1 ? blame_lines[map[i1]] :NULL , + (void *) (i1 !=-1 ? blame_lines[map[i1]] :NULL ), i2, map2[i2], - i2 !=-1 ? blame_lines[map2[i2]] :NULL ); + (void *) (i2 !=-1 ? blame_lines[map2[i2]] :NULL ));
Gaah. This is another case of casting that I'm sure is technically correct, but that I wonder whether there is any machine that actually cares..
But at least in that case I suspect the cast _may_ be required due to different pointer representations.
Linus
Quoted 3 lines · Linus Torvalds
Quoted 1 line
While most of this patch fixes void-pointer arithmetic
This one I disagree with. Doing arithmetic on "void *" is _really_ useful,
Agreed: If you want to walk over the memory pointed to by a void-pointer you want to do that byte-wise in the great majority of all cases.
Quoted 2 lines
and I think most compilers end up supporting it either to be compatible with gcc, or just because it's hard to not do it.
Now this statement I don't agree with. (And by that I don't mean I assume it to be false.)
I didn't start writing the patch because I like C99 so much. In fact, in my opinion it introduces some possibilities I'd rather not have in C because people might actually use them. But by default the Sun cc complains about void-pointer arithmetic and I feel awkward to force the compiler to accept broken code.
Maybe Rene Scharfe's method (as used in the patch to git-tar-tree) is a good way around it? There are no explicit casts involved and standard- compliant compilers like it, too. The downside is that you have two variables for the same thing/memory.
Regards,
-octo
Signature · 4 lines
Florian octo Forster Hacker in training GnuPG: 0x91523C3D http://verplant.org/
Quoted 4 lines · Florian Forster
I didn't start writing the patch because I like C99 so much. In fact, in my opinion it introduces some possibilities I'd rather not have in C because people might actually use them. But by default the Sun cc complains about void-pointer arithmetic...
I am reasonably sympathetic to that, and judging from the number of lines the patch touches, I am not as strongly opposed to it as Linus seems to be.
Quoted 4 lines
Maybe Rene Scharfe's method (as used in the patch to git-tar-tree) is a good way around it? There are no explicit casts involved and standard- compliant compilers like it, too. The downside is that you have two variables for the same thing/memory.
I think Rene's patch makes sense primarily because the functions affected are small and we can easily see that the aliased input variable ("data") is not used -- IOW, there is no confusion.
If it were a big function and the code used one variable for some purpose and the other one for another purpose, it would be far worse than having to cast the same variable occasionally.
BTW, I think we would probably want to have this patch on top of Rene's patch. In all instances, the variable "buf" is of type "const char *" and the existing casts do not make sense to me.
builtin-tar-tree.c +3 -3
@@ -36,7 +36,7 @@ static void reliable_write(const void *ddie ("git-tar-tree: disk full?" ); } size -= ret; - buf = (char *) buf + ret; + buf += ret; } }
@@ -65,13 +65,13 @@ static void write_blocked(const void *damemcpy (block + offset, buf, chunk); size -= chunk; offset += chunk; - buf = (char *) buf + chunk; + buf += chunk;write_if_needed (); }while (size >=BLOCKSIZE ) {reliable_write (buf,BLOCKSIZE ); size -=BLOCKSIZE ; - buf = (char *) buf +BLOCKSIZE ; + buf +=BLOCKSIZE ; }if (size) {memcpy (block + offset, buf, size);
builtin-tar-tree.c
Quoted 7 lines · Junio C Hamano
BTW, I think we would probably want to have this patch on top of Rene's patch. In all instances, the variable "buf" is of type "const char *" and the existing casts do not make sense to me.
builtin-tar-tree.c +0
-0
Your patch reverts builtin-tar-tree.c to the version which is currently both in master and next, which I think is a good change. However, could it be avoided at merge time?
OT: I found the blobs 5c8a5f0 and 39a61b6 by guessing (they are builtin-tar-tree.c in pu and master, respectively). OK, that was easy. But is there a way to reversely look up an object without guessing, i.e. find out which commit(s) introduced a certain blob?
Thanks,
René
Quoted 11 lines · Rene Scharfe
builtin-tar-tree.c
Quoted 6 lines · Junio C Hamano
BTW, I think we would probably want to have this patch on top of Rene's patch. In all instances, the variable "buf" is of type "const char *" and the existing casts do not make sense to me.
builtin-tar-tree.c +0
-0Your patch reverts
builtin-tar-tree.cto the version which is currently both in master and next, which I think is a good change. However, could it be avoided at merge time?
Sorry for attributing those "casts [that] do not make sense to me" to you -- it is not your code but part of Florian's patch.
I think applying the patch in question on top of Florian's 11172e would be the most sensible, since that is currently the tip of ff/c99 topic branch whose early parts have been merged to "next" and the tip to "pu". When Linus feels as sympathetic as I do, we can pull the rest of ff/c99 branch to "next" and then eventually to "master" and the patch will be merged together without introducing the nonsense casts.
Another possibility is to amend the tip of ff/c99 topic branch, since it is not merged to "next" yet. I promised not to rewind "master" nor "next", but never made promises not to rewind "pu", so it is a fair game. I think it is simpler and cleaner, so that will be what I will do.
Quoted 5 lines
OT: I found the blobs 5c8a5f0 and 39a61b6 by guessing (they are
builtin-tar-tree.cin pu and master, respectively). OK, that was easy. But is there a way to reversely look up an object without guessing, i.e. find out which commit(s) introduced a certain blob?
You could do something like this (totally untested).
Going from the above "diff --git" index line you have object name abbreviations and pathnames as clues. To take advantage of it, you could use "git rev-list pu -- builtin-tar-tree.c" instead of unlimited list.
$ git rev-list pu |
git diff-tree -r --stdin --pretty |
perl -e '
my @lines = ();
sub flush_em {
my @found = ();
my @comment = ();
for my $l (@lines) {
if ($l !~ /^:/) {
push @comment, $l;
next;
}
for (@ARGV) {
if ($l =~ / $_/) {
push @found, $l;
last;
}
}
}
if (@found) {
print join("", @comment, @found);
}
@lines = ();
}
while (<STDIN>) {
if (/^commit [0-9a-f]{40}$/) { flush_em(); }
push @lines, $_;
}
flush_em();
' 39a61b6 5c8a5f0Quoted 7 lines · Junio C Hamano
I think applying the patch in question on top of Florian's 11172e would be the most sensible, since that is currently the tip of ff/c99 topic branch whose early parts have been merged to "next" and the tip to "pu". When Linus feels as sympathetic as I do, we can pull the rest of ff/c99 branch to "next" and then eventually to "master" and the patch will be merged together without introducing the nonsense casts.
Now, without asking Linus about this further, I felt sympathetic enough to decide that void-pointer arithmetic avoidance is not so bad (touches only 70 lines or so in 19 files); tonight's "next" should be compilable with the default Solaris compiler.