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

nvme-print-json: extern json object add functions #2487

Merged
merged 1 commit into from
Sep 16, 2024

Conversation

ikegami-t
Copy link
Contributor

Some additinal functions introduced and fix obj_add_uint_0nx error.

@ikegami-t
Copy link
Contributor Author

Separated the commit from the PR #2484.

@ikegami-t ikegami-t mentioned this pull request Sep 1, 2024
Comment on lines 4807 to 4808
for (i = 0; i < len; i++)
sprintf(&value[strlen(value)], "%02x", buf[len - i - 1]);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a quadratic algorithm for what should be linear. Can you just keep track of the current length in a local variable?

Reversing the bytes also seems a little weird, but I guess I don't know what the intended use case is.

Copy link
Contributor Author

@ikegami-t ikegami-t Sep 5, 2024

Choose a reason for hiding this comment

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

This is a quadratic algorithm for what should be linear. Can you just keep track of the current length in a local variable?

Just fix this as mentioned.

Reversing the bytes also seems a little weird, but I guess I don't know what the intended use case is.

Unfortunately I do not any idea to resovle this. This outputs the little endian bytes endian as a big endian string as below for example.

buf[0] = 0xaa;
buf[1] = 0xbb;
buf[2] = 0xcc;
buf[3] = 0xdd;
value = "0xddccbbaa"

Thank you for your reviewing.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not really looked into it, so just a stupid comment from my side on the output order. Shouldn't this be output how the spec defines it? IIRC everything as little endian? (if this statement is untrue, I blame my vacation...)

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree with @igaw. It's hard to say without knowing how this function is going to be used. (I would expect the functions to be added in the same patch set as users of the functions.) But for generic "print bytes" functionality, it seems like it would make sense to print the bytes in the order they appear. If the function is interpreting the bytes as an integer, it might make more sense to have "unit" in the function name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi,
For example the existing fw-activate-history command outputs the loge page GUID value as below as a big endian string.

tokunori@tokunori-desktop:~/nvme-cli$ nvme-build ocp fw-activate-history /dev/nvme0
Firmware History Log:
  log identifier:           194
  valid entries:            0
  entries:
  log page version:         1
  log page guid:            0xd11cf3ac8ab24de2a3f6dab4769a796d

Also the OCP specification is also describes same as below as D11CF3AC8AB24DE2A3F6DAB4769A796Dh.
Screenshot from 2024-09-09 23-13-59
So seems the ocp commands just follow the specification descritions.


_cleanup_free_ char *value = NULL;

if (!o || !k || !buf || !len)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it necessary to handle all these cases? I don't see the other JSON value add functions checking their arguments like this. Seems like this would be papering over a bug in the caller...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which do you suggest?

  1. Ignore the invalid value with the checking then the function caused a null pointer exception or any unexpected error. (I do not think this function behavior correct.)
  2. Change the void function to return any error by the checking?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed this as add a error message by the checking.

Copy link
Contributor

Choose a reason for hiding this comment

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

Logging the error at least is an improvement. But in what situation do we expect a caller to pass in these invalid values? Seems like unnecessary overhead to check them. If null pointers are passed, we will get a pretty clear segfault.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. Just fixed as below. Thank you.

	if (!buf || !len) {
		obj_add_str(o, k, "No information provided");
		return;
	}

return;
}

sprintf(value, "0x");
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this being overwritten by the hex characters of the first byte?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right. Sorry the fix was not correct. Thanks for your suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed the overwritten error.

@ikegami-t ikegami-t force-pushed the json-extern-func branch 2 times, most recently from 14cc520 to 8cc6169 Compare September 9, 2024 16:08
Some additinal functions introduced and fix obj_add_uint_0nx error.

Signed-off-by: Tokunori Ikegami <[email protected]>
@igaw igaw merged commit 668f51d into linux-nvme:master Sep 16, 2024
17 checks passed
@igaw
Copy link
Collaborator

igaw commented Sep 16, 2024

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.

3 participants