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

Add support enum and char types in Format-Hex cmdlet #8191

Merged
merged 3 commits into from
Jan 16, 2019

Conversation

iSazonov
Copy link
Collaborator

@iSazonov iSazonov commented Nov 6, 2018

PR Summary

Fix #3358

Now char and enum types (and arrays of these types) is supported.

PR Checklist

}
else if (isBool)
{
// bool is 4 bytes apparently
Copy link
Member

Choose a reason for hiding this comment

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

bool is 1 byte, not 4.

There is a difference between sizeof and Marshal.SizeOf, see See the sizeof documentation, also this blog.

BitConverter.GetBytes(bool) works on managed memory and returns a 1 byte array.

I do think this code should use the managed representation for primitive types.

If Format-Hex is enhanced to support an arbitrary ValueType, then we should probably have a discussion about which representation is more useful because in some cases, the unmanaged layout might be more useful.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We could add -Unmanaged switch and by default do managed output.

Copy link
Member

Choose a reason for hiding this comment

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

Possibly, but we could also check the attributes on the struct, e.g. most structs used for interop specify [StructLayout(LayoutKind.Sequential)].

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It looks unnecessarily complicated, especially since CoreCLR/CoreFX uses some tricks to align structures. Seems this attribute does not always mean interop.

Copy link
Member

Choose a reason for hiding this comment

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

So we agree that this should be 1 byte by default?

@stale
Copy link

stale bot commented Jan 6, 2019

This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days.
Thank you for your contributions.
Community members are welcome to grab these works.

@iSazonov
Copy link
Collaborator Author

iSazonov commented Jan 9, 2019

@SteveL-MSFT @daxian-dbw Could you please review the PR?

@stale stale bot removed the Stale label Jan 9, 2019
}
else if (isBool)
{
// bool is 4 bytes apparently
Copy link
Member

Choose a reason for hiding this comment

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

So we agree that this should be 1 byte by default?

@iSazonov
Copy link
Collaborator Author

Rebased to get latest CI's updates.

@iSazonov
Copy link
Collaborator Author

@vexx32 @daxian-dbw Could you please look the PR before we merge?

Copy link
Collaborator

@vexx32 vexx32 left a comment

Choose a reason for hiding this comment

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

Looks pretty solid! Couple comments I would love to clarify before we merge, if we can. 😄

if (baseType.IsArray)
{
baseType = baseType.GetElementType();
dynamic o = inputObject;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this used? I don't see this used anywhere after this code point.

Copy link
Collaborator Author

@iSazonov iSazonov Jan 11, 2019

Choose a reason for hiding this comment

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

In next line. Name is very short and not readable.
Renamed.

isBool = true;
}

var elementSize = Marshal.SizeOf(baseType);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I could be wrong, but wasn't one of @lzybkr's comments that this would return the wrong size for boolean values?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it is ok - only local bool var-s have 4 byte size, rest - 1 byte.

@iSazonov iSazonov merged commit 782ef99 into PowerShell:master Jan 16, 2019
@iSazonov iSazonov deleted the format-hex-primitive branch January 16, 2019 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log
Projects
None yet
4 participants