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 many warnings in libnczarr #2852

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

ZedThree
Copy link
Contributor

This fixes about 100 out of 130 warnings from libnczarr. The remaining 30-ish warnings are basically either:

  • code doing subtraction or iterating backwards, where changing e.g. the loop index to size_t would give UB
  • public function/type declarations, for example filter IDs

One thing that gives rise to a bunch of warnings is maxstrlen. It's declared as an int in NCZ_VAR_INFO_T, but used as size_t in several places. I couldn't work out if this is publicly accessible or not, or if setting it to a negative value had some semantic meaning. I think it would be safe to change to size_t consistently.

More consistent with similar structs, and generates fewer warnings
Some mix-up here: `mode_t` was used for some netCDF-specific modes,
and `int` used for some POSIX-specific modes
Try to be more consistent in use of `size_t` across internal library declarations
Copy link
Collaborator

@DennisHeimbigner DennisHeimbigner Jan 31, 2024

Choose a reason for hiding this comment

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

  1. ncconfigure.h is normally included by including config.h.
    config.h should be in zincludes.h
  2. zinfo->controls.flags &= (size64_t)(~noflags);
    should this be zinfo->controls.flags &= ~((size64_t)(noflags));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. Ah sorry, my editor automatically includes the most relevant header when inserting new types. It looks like nothing in zincludes.h is used directly which is probably why it chose ncconfigure.h instead.

  2. It's probably actually clearer to change the type of noflags itself, not sure why I didn't spot that, thanks

Copy link
Collaborator

@DennisHeimbigner DennisHeimbigner Jan 31, 2024

Choose a reason for hiding this comment

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

You will note that as a rule, we always declare variables at the beginning of a block e.g. {...}
There used to be compilers that complained about mid-code declarations. Not sure if that
is still true, but I still adhere to that convention.
For similar reasons, I never declare for-loop iterators in the loop e.g. I never use for(size_t i...).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is a limitation of C89 and relaxed in C99. It's supported by every compiler on Compiler Explorer (except for a 6502 specific compiler and one for TI calculators).

I would strongly suggest moving to this style when writing new code, as it can avoid bugs by reducing the scope of variables and preventing use of uninitialised values.

@DennisHeimbigner
Copy link
Collaborator

Changes to the NCZarr code may be a waste of time. I am in the process of adding
Zarr V3 support and it required a major refactor of the code.
Can someone refrest my memory about the gcc flag I need to use to detect all of these int <-> size_t problems.

@ZedThree
Copy link
Contributor Author

ZedThree commented Feb 2, 2024

The CMake debug build enables: -Wall -Wconversion. -Wconversion should also enable -Wsign-conversion, but not -Wsign-compare (probably also worth enabling at some point)

`loaded_plugins_max` is *not* the total number of loaded plugins, but
the max ID, and defaults to `-1`. This clashes with the change of the
index to `size_t`
Copy link
Collaborator

@DennisHeimbigner DennisHeimbigner left a comment

Choose a reason for hiding this comment

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

Why are those two not equivalent?

@ZedThree
Copy link
Contributor Author

@DennisHeimbigner GitHub isn't showing what you're commenting on, but if it's in reference to f8da95a, then it's due to implicit conversions:

loaded_plugins_max is not the total number of loaded plugins, but
the max ID, and defaults to -1. This clashes with the change of the
index to size_t

* main: (248 commits)
  Updated Release Notes.
  A bit of cleanup for now, more to follow, but moving on to other roadblocks.
  Comment out debugging messages, will remove before final merge.  Clean up the logic, remove some rough edges.
  Add a filter to process options and report deprecation warning.
  Remove dangling define in netcdf_meta.h template file.
  No effect other than to remove a personal annoyance I introduced in the first place.
  Updated the nc-config script with the following:
  adding deprecation error for usage of NETCDF_ENABLE_NETCDF4
  removing cmake alias variable for netcdf4
  removing script
  replacing something that removes semicolons
  another one
  fixing some that were missing?
  removing variables
  Replacing NC_USE_STATIC_CRT with NETCDF_USE_STATIC_CRT
  Replace NC_FIND_SHARED_LIBS with NETCDF_FIND_SHARED_LIBS
  Replace ENABLE_XGETOPT with NETCDF_ENABLE_XGETOPT
  Replace ENABLE_UNIT_TESTS with NETCDF_ENABLE_UNIT_TESTS
  Replace ENABLE_TESTS with NETCDF_ENABLE_TESTS
  Replace ENABLE_STRICT_NULL_BYTE_HEADER_PADDING with NETCDF_ENABLE_STRICT_NULL_BYTE_HEADER_PADDING
  ...
Also delete actually unused variable
@DennisHeimbigner
Copy link
Collaborator

Please do not do this. I am in the process of making a major refactor of libnczarr.
All of these changes will just make it harder for me to resolve the merge later.

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.

2 participants