-
Notifications
You must be signed in to change notification settings - Fork 729
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
Enforce const
Where Applicable
#1542
Comments
I like this proposal.
which can result in memory leak |
I like the name We need to add only what can't already be expressed. That is constness for the sds content. The pointer-constness can already be expressed. Coupling pointer-constness with content-constness is not necessary. It's not normal style in C. The pointer-constness is just about not overwriting a local variable. It's up to each function how it uses local variables IMO. It's possible can use const for int variables or any pointer types. It's a matter of style. |
I think that the decision to treat sds as a concrete type makes it more logical to enforce the pointer assignment as well. In all cases I can think of we do not want to change the "pointer" sds as well since than we lose the sds content as well. |
@ranshid Constness doesn't prevent a memory leak. Just let the variable run out of its scope and it leaks. {
sds_const a = sdsnew("one");
} What it does though is that it prevents reassignement of a variable, so code like this becomes illegal: sds_const *array = something();
sds_const s;
int i;
for (i = 0; i < n; i++) {
if (is_our_wanted_string(array[i])) {
s = array[i]; // error: assignment of read-only variable ‘s’
break;
}
}
printf("%s", s); We don't have this limitation for any other type. If we want to prevent reassignment of for example an const int * const array = something();
const int sum = 0;
for (i = 0; i < n; i++) {
sum = sum + array[i]; // error: assignment of read-only variable ‘sum’
// But just use `int sum`! It has nothing to do
// with the constness of `array`. It's per variable,
// not per the content.
} I hope these examples show the problem of tying pointer-constness (single-assignment) and content-constness (mutability) together. |
`sds` is a typedef of `char *`. `const sds` means `char * const`, i.e. a const-pointer to non-const content. More often, you would want `const char *`, i.e. a pointer to const-content. Until now, it's not possible to express that. This PR adds `const_sds` which is a pointer to const-content sds. To get a const-pointer to const-content sds, you can use `const const_sds`. In this PR, some uses of `const sds` are replaced by `const_sds`. We can use it more later. Fixes #1542 --------- Signed-off-by: Viktor Söderqvist <[email protected]>
I agree with you. I don't think we should apply constness to the pointer, which is a separate concern and usually not a big issue. The main point is to enable static checks for unintended data modifications and compile time optimizations. It is also not about preventing memory leaks. I like const_sds too. |
I think that there is not much concern dropping the constness on the pointer level (as we just did) when you define something as const, you want to ensure the content is not changed in anyway (even by reassigning the value) which is currently not guaranteed |
`sds` is a typedef of `char *`. `const sds` means `char * const`, i.e. a const-pointer to non-const content. More often, you would want `const char *`, i.e. a pointer to const-content. Until now, it's not possible to express that. This PR adds `const_sds` which is a pointer to const-content sds. To get a const-pointer to const-content sds, you can use `const const_sds`. In this PR, some uses of `const sds` are replaced by `const_sds`. We can use it more later. Fixes valkey-io#1542 --------- Signed-off-by: Viktor Söderqvist <[email protected]> Signed-off-by: proost <[email protected]>
`sds` is a typedef of `char *`. `const sds` means `char * const`, i.e. a const-pointer to non-const content. More often, you would want `const char *`, i.e. a pointer to const-content. Until now, it's not possible to express that. This PR adds `const_sds` which is a pointer to const-content sds. To get a const-pointer to const-content sds, you can use `const const_sds`. In this PR, some uses of `const sds` are replaced by `const_sds`. We can use it more later. Fixes valkey-io#1542 --------- Signed-off-by: Viktor Söderqvist <[email protected]>
it would be ideal if we express the "constness" of the returned sds string explicitly. Similar callouts to other helper functions returning plain pointers. That said, since we have never enforced "constness" in the past in our code base I suspect this could turn into quite a code churn. So definitely not a blocker for this PR (and it is primarily for code hygiene).
Originally posted by @PingXie in #1312 (comment)
The text was updated successfully, but these errors were encountered: