From: Guillem Jover Date: Tue, 22 Jan 2008 08:31:32 +0000 (+0200) Subject: Use offsetof() instead of ad-hoc calculations X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=411e1d5de68770a58aaaffc4b6c2a87bb4dffbc1;p=dpkg Use offsetof() instead of ad-hoc calculations --- diff --git a/ChangeLog b/ChangeLog index d8d5f2e8..527cb67e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2008-01-22 Guillem Jover + + * lib/dpkg.h [!offsetof] (offsetof): New compatibilty macro. + * lib/parsedump.h (PKGIFPOFF): Use offsetof instead of ad-hoc + calculation. + (FILEFOFF): Likewise. + (struct fieldinfo): Change integer member type to size_t. + * lib/tarfn.c (TarChecksumOffset): Use offsetof instead of ad-hoc + calculation. + 2008-01-22 Guillem Jover * configure.ac (AC_CHECK_HEADERS): Check for 'stddef.h'. diff --git a/lib/dpkg.h b/lib/dpkg.h index 8d36fc88..d0e15fe9 100644 --- a/lib/dpkg.h +++ b/lib/dpkg.h @@ -414,6 +414,10 @@ void unsetenv(const char *x); /*** other compatibility functions ***/ +#ifndef offsetof +#define offsetof(st, m) ((size_t)&((st *)NULL)->m) +#endif + #ifndef HAVE_STRTOUL #define strtoul strtol #endif diff --git a/lib/parsedump.h b/lib/parsedump.h index f767b299..2f7bb769 100644 --- a/lib/parsedump.h +++ b/lib/parsedump.h @@ -25,12 +25,10 @@ struct fieldinfo; -#define PKGIFPOFF(f) ((char*)(&(((struct pkginfoperfile *)0x1000)->f)) - \ - (char*)(struct pkginfoperfile *)0x1000) +#define PKGIFPOFF(f) (offsetof(struct pkginfoperfile, f)) #define PKGPFIELD(pifp,of,type) (*(type*)((char*)(pifp)+(of))) -#define FILEFOFF(f) ((char*)(&(((struct filedetails *)0x1000)->f)) - \ - (char*)(struct filedetails *)0x1000) +#define FILEFOFF(f) (offsetof(struct filedetails, f)) #define FILEFFIELD(filedetail,of,type) (*(type*)((char*)(filedetail)+(of))) typedef void freadfunction(struct pkginfo *pigp, struct pkginfoperfile *pifp, @@ -56,7 +54,7 @@ struct fieldinfo { const char *name; freadfunction *rcall; fwritefunction *wcall; - unsigned int integer; + size_t integer; }; void parseerr(FILE *file, const char *filename, int lno, FILE *warnto, int *warncount, diff --git a/lib/tarfn.c b/lib/tarfn.c index 0a96e75f..397fedc4 100644 --- a/lib/tarfn.c +++ b/lib/tarfn.c @@ -34,8 +34,7 @@ struct TarHeader { }; typedef struct TarHeader TarHeader; -static const unsigned int TarChecksumOffset - = (unsigned int)&(((TarHeader *)NULL)->Checksum); +static const size_t TarChecksumOffset = offsetof(TarHeader, Checksum); /* Octal-ASCII-to-long */ static long