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

Avoid ID clashes #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Avoid ID clashes #75

wants to merge 1 commit into from

Conversation

cgzones
Copy link
Contributor

@cgzones cgzones commented Oct 26, 2024

Currently the the ID for a new paste is randomly generated in the caller of the database insert() function. Then the insert() function tries to insert a new row into the database with that passed ID. There can however already exists a paste in the database with the same ID leading to an insert failure, due to a constraint violation due to the PRIMARY KEY attribute. Checking prior the the INSERT via a SELECT query would open the window for a race condition.

A failure to push a new paste is quite severe, since the user might have spent some some to format the input.

Generate the ID in a loop inside, until the INSERT succeeds.

@matze
Copy link
Owner

matze commented Oct 26, 2024

An ID clash is indeed an extreme issue but perhaps we can solve it differently by constructing an INSERT query that generates a random primary key?

@cgzones
Copy link
Contributor Author

cgzones commented Oct 28, 2024

There exists a sqlite built-in functionrandom(and randomblob), but this would not solve this issue, cause the function might still generate a result already existent in the database.
Also we need the ID for the URL path generation, so this approach would require a secondary SQL query to get the ID in all cases (also non-clashing).

Currently the the ID for a new paste is randomly generated in the caller
of the database insert() function.  Then the insert() function tries to
insert a new row into the database with that passed ID.  There can
however already exists a paste in the database with the same ID leading
to an insert failure, due to a constraint violation due to the PRIMARY
KEY attribute.  Checking prior the the INSERT via a SELECT query would
open the window for a race condition.

A failure to push a new paste is quite severe, since the user might have
spent some some to format the input.

Generate the ID in a loop inside, until the INSERT succeeds.
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