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

Is it safe enough to use refGoBytes() #140

Closed
aaronyuan opened this issue Jan 26, 2024 · 3 comments
Closed

Is it safe enough to use refGoBytes() #140

aaronyuan opened this issue Jan 26, 2024 · 3 comments

Comments

@aaronyuan
Copy link

Hi, in the Get() function, it uses refGoBytes() to convert []bytes to C.char, involving the use of unsafe.Pointer. Is it possible that a key has been garbage collected, posing a risk of cKey pointing to null?

@linxGnu
Copy link
Owner

linxGnu commented Jan 30, 2024

Hi @aaronyuan

Yes, it's safe to use since the key is only GC-ed when run out of scope.

@aaronyuan
Copy link
Author

Hi @linxGnu
C.rocksdb_get(db.c, opts.c, cKey, C.size_t(len(key)), &cValLen, &cErr)
Does calling into C go beyond the scope of Go?

@linxGnu
Copy link
Owner

linxGnu commented Feb 12, 2024

@aaronyuan

Here is the signature of rocksdb_get:

extern ROCKSDB_LIBRARY_API char* rocksdb_get(
    rocksdb_t* db, const rocksdb_readoptions_t* options, const char* key,
    size_t keylen, size_t* vallen, char** errptr);

const char* means: key is immutable. After returning, RocksDB will not keep reference to the key. And again, the key is only GC-ed when run out of scope -> Safe!

@linxGnu linxGnu closed this as completed Mar 21, 2024
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

No branches or pull requests

2 participants