Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the signedness of char an interface issue?

Suppose I have a function

void foo(char *)

which, internally, needs to treat its input as a block of NUL-terminated bytes (say, it's a hash function on strings). I could cast the argument to unsigned char* in the function. I could also change the declaration to

void foo(unsigned char *)

Now, given that char, signed char and unsigned char are three different types, would this constitute an interface change, under any reasonable definition of the term "interface" in C?

(This question is intended to settle a discussion raised by another question. I have my opinions, but will not accept an answer until one comes up as a "winner" by the votes of others.)

like image 384
Fred Foo Avatar asked Jan 22 '23 05:01

Fred Foo


2 Answers

According to ISO/IEC 9899:TC3,

  • calling a function through an expression of incompatible type is undefined behaviour (6.5.2.2 §9)
  • compatible function types must have compatible parameter types (6.7.5.3 §15)
  • compatible pointer types must point to compatible types (6.7.5.1 §2)
  • char, signed char and unsigned char are different basic types (6.2.5 §14) and thus incompatible (6.2.7 §1), which is also explicitly mentioned in footnote 35 on page 35

So yes, this is clearly a change to the programming interface.

However, as char *, signed char * and unsigned char * will have identical representations and alignment requirements in any sane implementation of the C language, the binary interface will remain unchanged.

like image 116
Christoph Avatar answered Jan 23 '23 18:01

Christoph


Yes it is. Client code which previously compiled will no longer compile (or anyway is likely to generate new warnings), so this is a breaking change.

like image 39
2 revs Avatar answered Jan 23 '23 20:01

2 revs