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

Fix -Wrestrict in vtkFSSurfaceAnnotationReader #11

Open
jcfr opened this issue Mar 14, 2023 · 1 comment
Open

Fix -Wrestrict in vtkFSSurfaceAnnotationReader #11

jcfr opened this issue Mar 14, 2023 · 1 comment

Comments

@jcfr
Copy link
Contributor

jcfr commented Mar 14, 2023

/tmp/SlicerFreeSurfer/FreeSurfer/vtkFSSurfaceAnnotationReader.cxx: In member function ‘int vtkFSSurfaceAnnotationReader::ReadFSAnnotation()’:
/tmp/SlicerFreeSurfer/FreeSurfer/vtkFSSurfaceAnnotationReader.cxx:353:26: warning: passing argument 1 to restrict-qualified parameter aliases with argument 3 [-Wrestrict]
  353 |           sprintf (this->NamesList, "%s%3d {%s} ", this->NamesList,
      |                    ~~~~~~^~~~~~~~~  

If copying takes place between objects that overlap as a result of a call to sprintf() or snprintf(), the results are undefined.

Source: https://stackoverflow.com/a/1973595/1539918

And answer from ChatGPT:

This warning suggests that there is a potential issue with the use of the restrict keyword in the function argument, and it is causing an aliasing problem.

The restrict keyword is a C99 feature that tells the compiler that a particular pointer argument is not aliased by any other pointer in the same scope. In other words, if you use the restrict keyword, you're telling the compiler that the pointer passed as an argument does not point to the same memory location as any other pointer passed to the same function.

In your case, passing this->NamesList as the first argument to sprintf with %s format specifier is causing the warning. The %s format specifier expects a null-terminated string, and since the sprintf function can write to the memory location pointed to by this->NamesList, it is possible that it can also write to memory locations pointed to by other pointers. This violates the restrict keyword, which is meant to prevent such aliasing.

To fix this warning, you should consider using a different approach to write to this->NamesList. You can either remove the restrict keyword from the function argument, or you can use a temporary buffer to store the formatted string and then copy it to this->NamesList using strcpy or strncpy. Here is an example of the second approach:

char tempBuffer[256]; // temporary buffer to store the formatted string
sprintf(tempBuffer, "%s%3d {%s} ", this->NamesList, someNumber, someString);
strncpy(this->NamesList, tempBuffer, sizeof(this->NamesList));
this->NamesList[sizeof(this->NamesList)-1] = '\0'; // make sure the string is null-terminated

In this example, we use sprintf to format the string into tempBuffer, and then we use strncpy to copy the formatted string to this->NamesList. We also make sure that the string is null-terminated by setting the last character to '\0'. This approach avoids the aliasing issue and should fix the warning.

@lassoan
Copy link
Contributor

lassoan commented Mar 14, 2023

This freesurfer file reader is all very old, low quality code. We just moved it out of Slicer core but did not review or rework it. If we touch it then we should modernize it a bit to use STL strings and streams.

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

No branches or pull requests

2 participants