Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vcf.h: Remove const specifiers that are not honoured #274

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

noporpoise
Copy link
Contributor

const specifiers were being ignored in the header, resulting in many warnings in third party code compiling against htslib.

libs/htslib/htslib/vcf.h:880:24: warning: cast from 'const unsigned char *' to 'unsigned char *' drops const qualifier [-Wcast-qual]
    *q = (uint8_t*)p + 1;

@mp15
Copy link
Member

mp15 commented Sep 22, 2015

Surely it would be better just to fix the cast to:

    *q = (const uint8_t*)p + 1;

It's just pointer arithmetic so it's not modifying the value.

@noporpoise
Copy link
Contributor Author

Then you need to change the type of parameter q to const uint8_t **q, which gives warnings about each time it's called in htslib. I haven't check each of them to see if they need non-const q.

The problem is the functions bcf_dec_int1(), bcf_dec_typed_int1() and bcf_dec_size() take parameters const uint8_t *p and uint8_t **q, where p is const and q is not. They then set *q to point to p with some offset. This violates const correctness. If the const is not being obeyed it should be removed.

@jmarshall
Copy link
Member

This is the classic C-function-wants-to-work-on-either-const-or-non-const-strings problem as exemplified by strchr: in C strchr provides the same escape hatch from const char * to char * as these functions do (and is similarly not pedantically-const-correct), whereas in C++ there are two separate functions overloaded.

It's made worse here by having the output string passed by reference as a parameter rather than returned.

There are various solutions:

  1. Don't compile htslib headers with -Wcast-qual. If htslib headers are being #included as system headers then this is (probably) trivial; otherwise please don't tell us why your coding standards say you can't turn -Wcast-qual off as we already understand the arguments 😄
  2. If HTSlib did not have these functions inline in the public header file, then you could compile your own application code with -Wcast-qual without triggering the diagnostic on these functions.
  3. HTSlib could provide separate const and non-const versions of these functions. As we don't have function overloading in C, they would need to have different names. (We might find that we only currently need one of each pair… currently (at a glance) most uses in vcf.c are non-const, so this PR would suffice — at the cost of being a pain for future code that tries to be const-correct.)

@noporpoise
Copy link
Contributor Author

Thanks @jmarshall that all makes sense. Solution 2 seems like the best solution: it won't change the API and we don't have to adjust our compile options to satisfy htslib.

@noporpoise
Copy link
Contributor Author

Closing in favour of #275.

@noporpoise noporpoise closed this Sep 23, 2015
@noporpoise noporpoise reopened this Sep 23, 2015
@noporpoise
Copy link
Contributor Author

Reopening, inlining is required so removing const hopefully satisfies everyone. If anyone needs to use with const uint8_t*, they can cast to uint8_t*.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants