const question

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/OpenBSC@lists.osmocom.org/.

Neels Hofmeyr nhofmeyr at sysmocom.de
Tue Jan 26 14:01:45 UTC 2016


Hi,

my attention was directed at the gprs_shift_v_fixed() function:

  int gprs_shift_v_fixed(uint8_t **data, size_t *data_len,
                         size_t len, uint8_t **value)

It advances the *data pointer and returns a *value pointer.
So it could technically be consting much more aggressively.
However, the compiler warnings confuse me.

I've created this test code to illustrate; the ints should be treated read-only
by func():

  void func(const int **yyy)
  {
          (*yyy) ++;
  }

  int main(void)
  {
          int y[2] = {23, 42}; // mutable "by coincidence"
          int *yy = y;
          func(&yy);

          return *yy; // returns 42
  }

The idea is that yy is a pointer to a mutable int. func() expects a const int,
but what harm could be done by passing a mutable-int pointer as an
immutable-int pointer. Apparently this harm:

  check.c: In function ‘main’:
  check.c:10:14: warning: passing argument 1 of ‘func’ from incompatible pointer type
           func(&yy);
                ^
  check.c:1:6: note: expected ‘const int **’ but argument is of type ‘int **’
   void func(const int **yyy)

Changing yy to a pointer-to-const-int clears the warning:

          const int *yy = y;
          func(&yy);

I'm expecting the const to apply to the int pointed at, so I should be able to
modify the pointer while the compiler shouldn't waste a thought at whether the
int pointed at is const or not. func() is stricter, not less strict than y[].

To further illustrate, this would make the pointer address itself immutable,
which is not what I want:

          int * const yy = y;

I just want to declare the underlying int data to be unchangeable by func(),
not caring whether the caller passes a const int or a mutable int buffer.

Is the compiler merely nitpicking or am I getting something wrong
fundamentally?

Thanks!

~Neels

-- 
- Neels Hofmeyr <nhofmeyr at sysmocom.de>          http://www.sysmocom.de/
=======================================================================
* sysmocom - systems for mobile communications GmbH
* Alt-Moabit 93
* 10559 Berlin, Germany
* Sitz / Registered office: Berlin, HRB 134158 B
* Geschäftsführer / Managing Directors: Holger Freyther, Harald Welte
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.osmocom.org/pipermail/openbsc/attachments/20160126/e3299c90/attachment.bin>


More information about the OpenBSC mailing list