Skip to content

Raise the warning level to -Wall -Wextra and resolve the warnings - #609

Open
matejk wants to merge 3 commits into
LinearTapeFileSystem:release/v2.4.9.0from
matejk:cleanup/warnings
Open

Raise the warning level to -Wall -Wextra and resolve the warnings#609
matejk wants to merge 3 commits into
LinearTapeFileSystem:release/v2.4.9.0from
matejk:cleanup/warnings

Conversation

@matejk

@matejk matejk commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

The build used the default warning level, which hides real defects. This PR adds -Wall -Wextra to AM_CFLAGS and resolves every warning the tree then produces — string truncation in MAM attribute handling, sign and width mismatches, unused parameters and variables, missing field initializers — without behavior changes. Genuine bugs discovered by the higher warning level are filed in the separate bugfix PRs rather than here, so this PR stays mechanical and reviewable.

Rebased onto the v2.4.8.4 release: that release independently fixed some of the same unchecked-asprintf warnings (in xattr.c, ltfs_internal.c, filedebug_tc.c), so those hunks are now dropped; this PR retains the -Wall -Wextra enablement and the remaining fixes (tape.c, ltfstrace.c, xml_writer.c, xml_writer_libltfs.c, itdtimg_tc.c, sg_tape.c, sg_scsi_tape.c, arch_info.c, fs.c, ltfs_fsops.c, tape_ops.h).

Verified warning-clean at -Wall -Wextra -O2 with gcc on Ubuntu and clang on macOS (arm64); make check stays green.

Fixes #280.

@vandelvan
vandelvan requested review from XV02, madjesc and vandelvan June 12, 2026 20:54
matejk added 3 commits June 17, 2026 22:10
- Check or deliberately consume warn_unused_result returns: asprintf
  results are verified instead of testing the (undefined-on-failure)
  output pointer; the lost-and-found path no longer leaks or reuses
  the old path on reallocation; the trace dump consumes write results
  while staying best-effort; /proc reads fall back to a placeholder
  and leave room for the terminator and arch suffix.
- Replace strncpy on fixed-width SCSI and MAM fields with memcpy or
  the new bounded ltfs_string_copy (tape_ops.h), which always
  terminates; the device-list and serial-number copies were not
  guaranteed to be terminated before. The device-provided serial
  length is clamped to the field.
- The MAM coherency signature copies its terminator explicitly; the
  reader checks all five bytes.
- Initialize values read on early-exit paths (u_get_truncate_size).
- Use size_t for the name-length arithmetic in fs_dentry_lookup; the
  unsigned conversion also surfaced and fixed a reversed loop bound
  in the cleanup path.
- Spell the fall-through comment so the compiler recognizes it;
  format the XML timestamp with a real snprintf instead of the
  size-discarding arch_sprintf.

Addresses the -Wstringop-truncation reports of LinearTapeFileSystem#280.
unused-parameter and missing-field-initializers stay disabled:
callback signatures must keep parameters they do not use, and the
operation/option tables intentionally rely on zero-initialized
remaining members.

The tree builds without compiler warnings on Linux (gcc 15, -O0 and
-O2) and macOS (clang) apart from macOS SDK deprecation notices. The
CMake build carries the same flags.
@matejk
matejk force-pushed the cleanup/warnings branch from cd5b87f to 699b485 Compare June 17, 2026 20:35
@vandelvan
vandelvan requested a review from Piloalucard July 3, 2026 19:29
@vandelvan
vandelvan changed the base branch from main to release/v2.4.9.0 August 5, 2026 21:06

@XV02 XV02 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for your work in making the compilation more strict. I'm generally okay with the change but I think the solutions proposed to eliminate the new found errors and warnings may need more work. Thanks :DD

Comment thread .gitignore
Comment on lines +35 to +36
compile_commands.json
.cache/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems like a change done for the use of Cmake, should be moved to that PR since it's also not there and removed from here since it's unrelated.

Comment thread configure.ac
dnl must keep parameters they do not use.
dnl -Wno-missing-field-initializers: designated initializers of operation and
dnl option tables intentionally leave the remaining members zeroed.
AM_CFLAGS="-Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wsign-compare -fsigned-char ${FUSE_MODULE_CFLAGS} ${UUID_MODULE_CFLAGS} ${LIBXML2_MODULE_CFLAGS} ${ICU_MODULE_CFLAGS} ${SNMP_ENABLE} ${SNMP_MODULE_CFLAGS}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This change overwrite whatever AM_CFLAGS contained before that could be carried from the environment. As the code below this and many others you should first add ${AM_CFLAGS} before adding others to avoid the overwrite.

Comment thread src/libltfs/ltfstrace.c
Comment on lines +225 to +234
/* The trace dump is best-effort diagnostics; a write failure must not fail
* the operation that triggered the dump. glibc marks write() with
* warn_unused_result and a (void) cast does not silence it, so consume the
* result here and deliberately ignore it. */
static void _dump_write(int fd, const void *buf, size_t count)
{
ssize_t ignored = arch_write(fd, buf, count);
(void)ignored;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This kind of solution that is just an static file single-wrapper and it's not done system wide seems a little bit overengineered. It hides the intent from the call sites and can't be reused anywhere else. A more idiomatic approach should be used like (void)!arch_write(...) at the call sites instead.

Comment thread src/libltfs/tape.c
Comment on lines +3005 to +3015
/* MAM attribute fields have fixed widths; truncation is intentional and
* parse_vol() pads and terminates afterwards. */
static void mam_field_copy(char *dest, size_t field_size, const char *src)
{
size_t n = strlen(src);

if (n > field_size)
n = field_size;
memcpy(dest, src, n);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same issue, single file solutions are not the best solutions, this also doesn't null terminate making the use of the function dependent on parse_vol() being called afterwards and not reusable. memcpy should be used instead in place instead of a custom wrapper. This also duplicates semantics already used in arch_strncpy.

Comment thread src/libltfs/tape_ops.h
Comment on lines +68 to +76
/* Bounded, always-terminated string copy for the fixed-width identifier
* fields below; truncation of longer sources is intentional. */
static inline void ltfs_string_copy(char *dest, size_t dest_size, const char *src)
{
size_t n = strnlen(src, dest_size - 1);

memcpy(dest, src, n);
dest[n] = '\0';
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This has the same issue, just a little bit more wide use. Also it adds a third signature to the copy functions of the codebase making it harder for other contributors at knowing which one to use. This can be solved using other more idiomatic approaches like sprintf, or any other. Prefer to use those instead or find a solution that doesn't require a custom copy function.

Comment thread src/libltfs/ltfs_fsops.c
Comment on lines +2070 to +2071
memcpy(work_buf+len+1, token, strlen(token)); /* "/aaa/ccc\0" */
work_buf[len+1+strlen(token)] = '\0';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This adds a new problem because now the bound put by work_buf_len is not there, making it possible for a buffer overwrite risk, consider other solution for the warning.

Comment thread src/libltfs/ltfs_fsops.c
Comment on lines +2078 to +2079
memcpy(work_buf+len+1, token, strlen(token)); /* "/aaa/ccc/target.txt\0" */
work_buf[len+1+strlen(token)] = '\0';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ditto about the problem with dropping work_buf_len.

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.

Linux compilation warnings(4)

2 participants