🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4278

Re: Variable length struct hack

#4278
From: David Adrien Tanguay
Date: Thu, 26 Oct 1995 12:14
23 lines
1238 bytes
How is the following concept for strict conformance? (with appropriate context)

struct s_t {
	int this, that;
	/* this structure has a variable length tail */
};

#define tail_of(p) ((char*) ((p)+1))

void f(size_t tail_size) {
	struct s_t *sp = malloc(sizeof(*sp) + tail_size);
	char *the_tail_is = tail_of(sp);
	...
}

If tail_size < sizeof(struct s_t), can tail_of() fail because of the +1?
I.e., assuming pointers carry bounds, can the implicit cast from malloc to sp
re-boundize the pointer value to an integral number of struct s_t's? Then the
cast to char* in tail_of would produce a pointer with the truncated bounds so
you still couldn't access the entire malloc'd object that way.
-- 
David Tanguay       dat@Thinkage.on.ca       http://www.thinkage.on.ca/~dat/
Thinkage, Ltd.           Kitchener, Ontario, Canada          [43.24N 80.29W]
Technical Details
Message-ID: <DH220v.9r@thinkage.on.ca>
Path: arc1-ca.pugleaf.net!thinkage.on.ca!dat
References: <463egk$jbb@nntp.Stanford.EDU> <DH0uG2.Ft@eskimo.com>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!thinkage.on.ca!dat
From: dat@thinkage.on.ca (David Adrien Tanguay)
Subject: Re: Variable length struct hack
Message-ID: <DH220v.9r@thinkage.on.ca>
Sender: news@thinkage.on.ca
Organization: Thinkage Ltd., Kitchener, Ontario
References: <463egk$jbb@nntp.Stanford.EDU> <DH0uG2.Ft@eskimo.com>
Date: Thu, 26 Oct 1995 12:14:54 GMT