-
Notifications
You must be signed in to change notification settings - Fork 380
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
refactor sshfx encoding, fix link rot, go fmt #545
Conversation
*b = Buffer{ | ||
b: b.b[:0], | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This syntax clears off
and Err
implicitly.
if b.Len() < 1 { | ||
return 0, ErrShortPacket | ||
b.off = len(b.b) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We set the b.off
to len(b.b)
before every b.Err = ErrShortPacket
so that say, we’re unmarshalling a uint32, and that fails because there are only 3 bytes left, but then we unmarshal a uint8, which would succeed and consume one of the three remaining bytes.
We also if b.Err != nil { … }
as preamble of each Consume…
function, but it’s better to have safety in layers, rather than rely on only one single failsafe.
} | ||
|
||
v := b.b[b.off:] | ||
if len(v) > int(length) { | ||
if len(v) > length || cap(v) > length { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized, we should ensure the cap
is clamped the same as len
.
// | ||
// If hint has sufficient capacity to hold the data, it will be reused and overwritten, | ||
// otherwise a new backing slice will be allocated and returned. | ||
func (b *Buffer) ConsumeByteSliceCopy(hint []byte) []byte { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out, we were using this code twice, but it was also wholly reallocating if len
was insufficient, even though maybe it has the available cap
. So, using the grow slice trick instead, so maybe we have a chance to reuse hint
even if the len
is insufficient, but the cap
is sufficient.
package filexfer | ||
package sshfx |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the expected name of this package, so that importers know the preferred name it should be renamed to.
@@ -122,3 +122,48 @@ func (f PacketType) String() string { | |||
return fmt.Sprintf("SSH_FXP_UNKNOWN(%d)", f) | |||
} | |||
} | |||
|
|||
func newPacketFromType(typ PacketType) (Packet, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function makes way more sense here, rather than in packets.go
.
} | ||
|
||
// ReadPacket defines the SSH_FXP_READ packet. | ||
type ReadPacket struct { | ||
Handle string | ||
Offset uint64 | ||
Len uint32 | ||
Length uint32 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Name change to better mirror standard.
@@ -4,54 +4,54 @@ import ( | |||
sshfx "github.com/pkg/sftp/internal/encoding/ssh/filexfer" | |||
) | |||
|
|||
const extensionPosixRename = "[email protected]" | |||
const extensionPOSIXRename = "[email protected]" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
POSIX
is an all-caps abbreviation that should be all-caps in variable names. (Are you glad we had this all in internal
so we could change this bad early draft choice? )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took a quick look and it looks ok. I don't have time for a full review, sorry. In general I may reduce my involvement in open source in the coming months.
It’s ok. I’ve had to scale back from time to time as well. Especially, when a job comes in that doesn’t have a synergy with working on the project. |
I’ve poked around with the wire formatting code, and switched from a “check error every time” to “accumulate an error” which allows for more direct and concise unmarshals in general.
Also, I found the ietf draft link rot #544 and updated each of the ones that points to a version earlier than
-13
. (The-13
links are still valid.)Also, we’ve accumulated some
go fmt
changes that show up in go 1.20, so I threw those in as well.