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

Return a negative parameter description if no parameters have been described while parsing #52

Open
jeroenrinzema opened this issue Dec 27, 2022 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@jeroenrinzema
Copy link
Owner

After debugging the issue (#49) reported by @kishaningithub. I noticed that it is possible to not define the parameters by simply sending a out of bound/negative integer when describing a prepared message. This could probably be embedded by updating the writeParameterDescriptions to return a negative integer when no parameters are defined and to update the simple query option to simply not return any parameters.

psql-wire/command.go

Lines 294 to 303 in 8f291e8

func (srv *Server) writeParameterDescriptions(writer *buffer.Writer, parameters []oid.Oid) error {
writer.Start(types.ServerParameterDescription)
writer.AddInt16(int16(len(parameters)))
for _, parameter := range parameters {
writer.AddInt32(int32(parameter))
}
return writer.End()
}

psql-wire/options.go

Lines 71 to 92 in 8f291e8

// NOTE: we have to lookup all parameters within the given query.
// Parameters could represent positional parameters or anonymous
// parameters. We return a zero parameter oid for each parameter
// indicating that the given parameters could contain any type. We
// could safely ignore the err check while converting given
// parameters since ony matches are returned by the positional
// parameter regex.
matches := QueryParameters.FindAllStringSubmatch(query, -1)
parameters := make([]oid.Oid, 0, len(matches))
for _, match := range matches {
// NOTE: we have to check whether the returned match is a
// positional parameter or an un-positional parameter.
// SELECT * FROM users WHERE id = ?
if match[1] == "" {
parameters = append(parameters, 0)
}
position, _ := strconv.Atoi(match[1]) //nolint:errcheck
if position > len(parameters) {
parameters = parameters[:position]
}
}

@jeroenrinzema jeroenrinzema added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Dec 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant