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

Make array & new_array const-correct in test_is_string_in_array. #223

Merged
merged 1 commit into from
Aug 2, 2024

Conversation

sp1ff
Copy link
Contributor

@sp1ff sp1ff commented Jul 12, 2024

When building on Arch, I saw a few compilation errors of the form:

string_lib_test.c:873:40: error: passing argument 2 of ‘IsStringInArray’ from incompatible pointer type [-Wincompatible-pointer-types]

This small commit fixes them.

@mender-test-bot
Copy link

There was an error running your pipeline, see logs for details.

Copy link
Contributor

@olehermanse olehermanse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor

@larsewi larsewi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🚀

One note:
Since the new_array variable is allocated on the heap and later free'd, it may be better to keep it char ** and instead explicitly cast it to const char ** when passing it as an argument to IsStringInArray. The reason is that free(3) takes a void * as an argument (not const void *).

@olehermanse
Copy link
Contributor

olehermanse commented Aug 2, 2024

Thanks 🚀

One note: Since the new_array variable is allocated on the heap and later free'd, it may be better to keep it char ** and instead explicitly cast it to const char ** when passing it as an argument to IsStringInArray. The reason is that free(3) takes a void * as an argument (not const void *).

No, how he did it is correct. This is a double pointer, strings is a non-const pointer, to a non-const pointer, to a const char in memory.

#include <stdio.h> // printf()                                                                                                                               
#include <stdlib.h> // calloc()                                                                                                                              

int main(void)
{
  const char **strings = calloc(3, sizeof(const char *));

  // C compiler is not very strict with string literals                                                                                                      
  strings[0] = "hello";	// This works regardless of whether strings has const or not                                                                         

  // After assigning to a typed variable, the compiler will be more strict:                                                                                    
  const char *world = "world";
  strings[1] = world; // Const correctness is checked here.                                                                                                  
  printf("%s, %s\n", strings[0], strings[1]);

  void *temp = strings;
  // const void *temp = strings; // This would not work, const mismatch.                                                                                     

  free(temp); // free(strings) would also work                                                                                                               
  return 0;
}
$ gcc -Wall -Werror -Wpedantic test.c -o test && ./test
hello, world

@olehermanse olehermanse merged commit 4e45713 into NorthernTechHQ:master Aug 2, 2024
7 checks passed
@sp1ff
Copy link
Contributor Author

sp1ff commented Aug 8, 2024

Thanks-- works much better!

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.

4 participants