Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Distributing C as headers. Date: Sun, 27 Jan 2019 16:59:39 -0800 Organization: None to speak of Lines: 51 Message-ID: References: <3719d4fa-cf75-4a85-9255-b3f961a649c1@googlegroups.com> <5c4b5064$0$21586$426a74cc@news.free.fr> <1bh8dw2l5r.fsf@pfeifferfamily.net> <5c4c1691$0$21610$426a74cc@news.free.fr> <1bwomr17cp.fsf@pfeifferfamily.net> <%Hr3E.90867$nC1.33629@fx04.am4> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="6e9d60f1a86f6adea14a7c4a73c06923"; logging-data="8595"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+HVsoWOU79ZvF37UPI7oii" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:S0AVZJYuvVfXaNWs2rYzteTbiH4= sha1:v5U3rmcnBo2YfOu4Jy8v1IFwlHU= Xref: csiph.com comp.lang.c:140340 "Pascal J. Bourguignon" writes: [...] > A lot of C functions take a pointer and a size (or a length; Hello > off-by-one errors!). > > Instead they should take an object abstracting away those elements, so > that their consistency can be ensured by the functional abstraction. > > > strncpy(dst,src,size); /* crash ensured */ No, a crash is not ensured. Here's a complete C program that includes that line: #include #include int main(void) { char dst[100]; const char *src = "hello, world"; const size_t size = sizeof dst; strncpy(dst,src,size); puts(dst); } It will not crash. (I almost certainly wouldn't use strncpy myself, but that's not the point. It's certainly possible to use strncpy in a way that will cause a program to crash; that also is not the point.) > string_copy(dst,src); /* no crash possible, because string_copy > with handle all the cases, given > src->characters, src->length and > dst->characters, dst->allocated_size */ I don't believe there's a standard Pascal function called "string_copy". In fact you can simply assign strings in Pascal: dst := src; What exactly are "src->characters", "src->length", and so forth? As far as I can tell, Pascal doesn't use that syntax. You assert that your string_copy procedure can't crash, but you haven't shown us its implementation. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */