object-file.c: use "enum" return type for unpack_loose_header()
In a preceding commit we changed and documented unpack_loose_header() from its previous behavior of returning any negative value or zero, to only -1 or 0. Let's add an "enum unpack_loose_header_result" type and use it for these return values, and have the compiler assert that we're exhaustively covering all of them. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
01cab97679
commit
3b6a8db3b0
19
cache.h
19
cache.h
@ -1307,7 +1307,10 @@ int git_open_cloexec(const char *name, int flags);
|
|||||||
* unpack_loose_header() initializes the data stream needed to unpack
|
* unpack_loose_header() initializes the data stream needed to unpack
|
||||||
* a loose object header.
|
* a loose object header.
|
||||||
*
|
*
|
||||||
* Returns 0 on success. Returns negative values on error.
|
* Returns:
|
||||||
|
*
|
||||||
|
* - ULHR_OK on success
|
||||||
|
* - ULHR_BAD on error
|
||||||
*
|
*
|
||||||
* It will only parse up to MAX_HEADER_LEN bytes unless an optional
|
* It will only parse up to MAX_HEADER_LEN bytes unless an optional
|
||||||
* "hdrbuf" argument is non-NULL. This is intended for use with
|
* "hdrbuf" argument is non-NULL. This is intended for use with
|
||||||
@ -1315,9 +1318,17 @@ int git_open_cloexec(const char *name, int flags);
|
|||||||
* reporting. The full header will be extracted to "hdrbuf" for use
|
* reporting. The full header will be extracted to "hdrbuf" for use
|
||||||
* with parse_loose_header().
|
* with parse_loose_header().
|
||||||
*/
|
*/
|
||||||
int unpack_loose_header(git_zstream *stream, unsigned char *map,
|
enum unpack_loose_header_result {
|
||||||
unsigned long mapsize, void *buffer,
|
ULHR_OK,
|
||||||
unsigned long bufsiz, struct strbuf *hdrbuf);
|
ULHR_BAD,
|
||||||
|
};
|
||||||
|
enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
|
||||||
|
unsigned char *map,
|
||||||
|
unsigned long mapsize,
|
||||||
|
void *buffer,
|
||||||
|
unsigned long bufsiz,
|
||||||
|
struct strbuf *hdrbuf);
|
||||||
|
|
||||||
struct object_info;
|
struct object_info;
|
||||||
int parse_loose_header(const char *hdr, struct object_info *oi,
|
int parse_loose_header(const char *hdr, struct object_info *oi,
|
||||||
unsigned int flags);
|
unsigned int flags);
|
||||||
|
@ -1210,9 +1210,11 @@ void *map_loose_object(struct repository *r,
|
|||||||
return map_loose_object_1(r, NULL, oid, size);
|
return map_loose_object_1(r, NULL, oid, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
int unpack_loose_header(git_zstream *stream,
|
enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
|
||||||
unsigned char *map, unsigned long mapsize,
|
unsigned char *map,
|
||||||
void *buffer, unsigned long bufsiz,
|
unsigned long mapsize,
|
||||||
|
void *buffer,
|
||||||
|
unsigned long bufsiz,
|
||||||
struct strbuf *header)
|
struct strbuf *header)
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
@ -1229,13 +1231,13 @@ int unpack_loose_header(git_zstream *stream,
|
|||||||
status = git_inflate(stream, 0);
|
status = git_inflate(stream, 0);
|
||||||
obj_read_lock();
|
obj_read_lock();
|
||||||
if (status < Z_OK)
|
if (status < Z_OK)
|
||||||
return -1;
|
return ULHR_BAD;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check if entire header is unpacked in the first iteration.
|
* Check if entire header is unpacked in the first iteration.
|
||||||
*/
|
*/
|
||||||
if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
|
if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
|
||||||
return 0;
|
return ULHR_OK;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We have a header longer than MAX_HEADER_LEN. The "header"
|
* We have a header longer than MAX_HEADER_LEN. The "header"
|
||||||
@ -1243,7 +1245,7 @@ int unpack_loose_header(git_zstream *stream,
|
|||||||
* --allow-unknown-type".
|
* --allow-unknown-type".
|
||||||
*/
|
*/
|
||||||
if (!header)
|
if (!header)
|
||||||
return -1;
|
return ULHR_BAD;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* buffer[0..bufsiz] was not large enough. Copy the partial
|
* buffer[0..bufsiz] was not large enough. Copy the partial
|
||||||
@ -1264,7 +1266,7 @@ int unpack_loose_header(git_zstream *stream,
|
|||||||
stream->next_out = buffer;
|
stream->next_out = buffer;
|
||||||
stream->avail_out = bufsiz;
|
stream->avail_out = bufsiz;
|
||||||
} while (status != Z_STREAM_END);
|
} while (status != Z_STREAM_END);
|
||||||
return -1;
|
return ULHR_BAD;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *unpack_loose_rest(git_zstream *stream,
|
static void *unpack_loose_rest(git_zstream *stream,
|
||||||
@ -1429,13 +1431,19 @@ static int loose_object_info(struct repository *r,
|
|||||||
if (oi->disk_sizep)
|
if (oi->disk_sizep)
|
||||||
*oi->disk_sizep = mapsize;
|
*oi->disk_sizep = mapsize;
|
||||||
|
|
||||||
if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr),
|
switch (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr),
|
||||||
allow_unknown ? &hdrbuf : NULL) < 0)
|
allow_unknown ? &hdrbuf : NULL)) {
|
||||||
|
case ULHR_OK:
|
||||||
|
break;
|
||||||
|
case ULHR_BAD:
|
||||||
status = error(_("unable to unpack %s header"),
|
status = error(_("unable to unpack %s header"),
|
||||||
oid_to_hex(oid));
|
oid_to_hex(oid));
|
||||||
if (status < 0)
|
break;
|
||||||
; /* Do nothing */
|
}
|
||||||
else if (hdrbuf.len) {
|
|
||||||
|
if (status < 0) {
|
||||||
|
/* Do nothing */
|
||||||
|
} else if (hdrbuf.len) {
|
||||||
if ((status = parse_loose_header(hdrbuf.buf, oi, flags)) < 0)
|
if ((status = parse_loose_header(hdrbuf.buf, oi, flags)) < 0)
|
||||||
status = error(_("unable to parse %s header with --allow-unknown-type"),
|
status = error(_("unable to parse %s header with --allow-unknown-type"),
|
||||||
oid_to_hex(oid));
|
oid_to_hex(oid));
|
||||||
|
23
streaming.c
23
streaming.c
@ -229,17 +229,16 @@ static int open_istream_loose(struct git_istream *st, struct repository *r,
|
|||||||
st->u.loose.mapped = map_loose_object(r, oid, &st->u.loose.mapsize);
|
st->u.loose.mapped = map_loose_object(r, oid, &st->u.loose.mapsize);
|
||||||
if (!st->u.loose.mapped)
|
if (!st->u.loose.mapped)
|
||||||
return -1;
|
return -1;
|
||||||
if ((unpack_loose_header(&st->z,
|
switch (unpack_loose_header(&st->z, st->u.loose.mapped,
|
||||||
st->u.loose.mapped,
|
st->u.loose.mapsize, st->u.loose.hdr,
|
||||||
st->u.loose.mapsize,
|
sizeof(st->u.loose.hdr), NULL)) {
|
||||||
st->u.loose.hdr,
|
case ULHR_OK:
|
||||||
sizeof(st->u.loose.hdr),
|
break;
|
||||||
NULL) < 0) ||
|
case ULHR_BAD:
|
||||||
(parse_loose_header(st->u.loose.hdr, &oi, 0) < 0)) {
|
goto error;
|
||||||
git_inflate_end(&st->z);
|
|
||||||
munmap(st->u.loose.mapped, st->u.loose.mapsize);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
if (parse_loose_header(st->u.loose.hdr, &oi, 0) < 0)
|
||||||
|
goto error;
|
||||||
|
|
||||||
st->u.loose.hdr_used = strlen(st->u.loose.hdr) + 1;
|
st->u.loose.hdr_used = strlen(st->u.loose.hdr) + 1;
|
||||||
st->u.loose.hdr_avail = st->z.total_out;
|
st->u.loose.hdr_avail = st->z.total_out;
|
||||||
@ -248,6 +247,10 @@ static int open_istream_loose(struct git_istream *st, struct repository *r,
|
|||||||
st->read = read_istream_loose;
|
st->read = read_istream_loose;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
error:
|
||||||
|
git_inflate_end(&st->z);
|
||||||
|
munmap(st->u.loose.mapped, st->u.loose.mapsize);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user