Re: [CFT] BSDL iconv in base system

From: Dag-Erling Smørgrav <des_at_des.no>
Date: Wed, 16 Jun 2010 22:07:58 +0200
Jaakko Heinonen <jh_at_FreeBSD.org> writes:
> iconv(3) prototype doesn't conform to POSIX.1-2008. Is it a
> well-considered decision?

Probably not, because it breaks the interface.

Imagine that inbuf were just a char *, not a char **.  It would be
perfectly safe to change it to const char *, because you can always
assign a char * to a const char *.

However, inbuf is a char **, which is a pointer to a pointer to char.
Gabor changed it to const char **, which is a pointer to a pointer to
const char.  Unfortunately, the two types are incompatible.  If foo is a
char *, you can't pass &foo as inbuf.

% cat >/tmp/const.c <<EOF
#include <stdio.h>
void fs(char *s) { puts(++s); }
void gs(const char *s) { puts(++s); }
void fsp(char **sp) { puts(++*sp); }
void gsp(const char **sp) { puts(++*sp); }
int main() { char *s = "xyzzy", **sp = &s; fs(s); gs(s); fsp(sp); gsp(sp); }
EOF
% cc -Wall -Wextra -Werror -std=c99 -o/dev/null /tmp/const.c
cc1: warnings being treated as errors
/tmp/const.c: In function ‘main’:
/tmp/const.c:6: error: passing argument 1 of ‘gsp’ from incompatible pointer type
/tmp/const.c:5: note: expected ‘const char **’ but argument is of type ‘char **’

This means you can't, say, read data from a file into a buffer and then
pass that buffer to iconv, because the buffer is not const (otherwise
you couldn't have read data into it).  That seems like a pretty
fundamental flaw.

DES
-- 
Dag-Erling Smørgrav - des_at_des.no
Received on Wed Jun 16 2010 - 18:10:10 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:40:04 UTC