Skip to content

libnvme/json: check write return value for build warnings#3101

Merged
igaw merged 1 commit intolinux-nvme:masterfrom
ikegami-t:libnvme-json-fix
Feb 20, 2026
Merged

libnvme/json: check write return value for build warnings#3101
igaw merged 1 commit intolinux-nvme:masterfrom
ikegami-t:libnvme-json-fix

Conversation

@ikegami-t
Copy link
Copy Markdown
Contributor

Since the following build warnings output.

[61/232] Compiling C object libnvme/src/libnvme-test.so.p/nvme_json.c.o ../libnvme/src/nvme/json.c: In function ‘json_update_config’: ../libnvme/src/nvme/json.c:438:9: warning: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
438 | write(fd, "\n", 1);
| ^~~~~~~~~~~~~~~~~~
[62/232] Compiling C object libnvme/src/libnvme.so.3.0.0.p/nvme_json.c.o
../libnvme/src/nvme/json.c: In function ‘json_update_config’:
../libnvme/src/nvme/json.c:438:9: warning: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
438 | write(fd, "\n", 1);
| ^~~~~~~~~~~~~~~~~~

Fixes: 74b30d2 ("json: update nvme_dump_config to a file descriptor")

Comment thread libnvme/src/nvme/json.c
JSON_C_TO_STRING_NOSLASHESCAPE);
write(fd, "\n", 1);
if (ret < 0) {
if (ret < 0 || write(fd, "\n", 1) < 0) {
Copy link
Copy Markdown
Collaborator

@igaw igaw Feb 18, 2026

Choose a reason for hiding this comment

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

The log message could then be missleading when json_object_to_fd is succesfull and then the write fails. In this case json_util_get_last_error will not be correct.

something like this here would be less confusing

	ret = json_object_to_fd(fd, json_root,
				JSON_C_TO_STRING_PRETTY |
				JSON_C_TO_STRING_NOSLASHESCAPE);
	if (ret < 0) {
		nvme_msg(ctx, LOG_ERR, "Failed to write JSON config file: %s\n",
			 json_util_get_last_err());
		[...]
	};

	ret = write(...)
	if (ret < 0) {
		nvme_msg(...);
	}

(though highly unlikely to happen)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

How about this?

		nvme_msg(ctx, LOG_ERR, "Failed to write JSON config file: %s\n",
			  ret ? json_util_get_last_err() : strerror());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

yes, that's fine too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just fixed as commented. Thank you.

Since the following build warnings output.

[61/232] Compiling C object libnvme/src/libnvme-test.so.p/nvme_json.c.o
../libnvme/src/nvme/json.c: In function ‘json_update_config’:
../libnvme/src/nvme/json.c:438:9: warning: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  438 |         write(fd, "\n", 1);
      |         ^~~~~~~~~~~~~~~~~~
[62/232] Compiling C object libnvme/src/libnvme.so.3.0.0.p/nvme_json.c.o
../libnvme/src/nvme/json.c: In function ‘json_update_config’:
../libnvme/src/nvme/json.c:438:9: warning: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  438 |         write(fd, "\n", 1);
      |         ^~~~~~~~~~~~~~~~~~

Fixes: 74b30d2 ("json: update nvme_dump_config to a file descriptor")
Signed-off-by: Tokunori Ikegami <[email protected]>
@igaw igaw merged commit 9597544 into linux-nvme:master Feb 20, 2026
19 checks passed
@igaw
Copy link
Copy Markdown
Collaborator

igaw commented Feb 20, 2026

Thanks!

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