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

[FFI] Work with a pointer to a string #44017

Closed
listepo opened this issue Nov 2, 2020 · 3 comments
Closed

[FFI] Work with a pointer to a string #44017

listepo opened this issue Nov 2, 2020 · 3 comments

Comments

@listepo
Copy link

listepo commented Nov 2, 2020

I am facing a problem when working with strings. For example:
https://github.com/postgres/postgres/blob/8a15e735be00f156a7227741c0ce88702e6de099/src/interfaces/libpq/fe-connect.c#L6535

char *
PQpass(const PGconn *conn)
{
	char	   *password = NULL;

	if (!conn)
		return NULL;
	if (conn->connhost != NULL)
		password = conn->connhost[conn->whichhost].password;
	if (password == NULL)
		password = conn->pgpass;
	/* Historically we've returned "" not NULL for no password specified */
	if (password == NULL)
		password = "";
	return password;
}

when I use dart-lang/native#508
I get an error

art(11490,0x7000070ec000) malloc: *** error for object 0x10fcb0efc: pointer being freed was not allocated
dart(11490,0x7000070ec000) malloc: *** set a breakpoint in malloc_error_break to debug

I guess the error occurs when the function returns an empty string(password = "";) and I try to free memory.(for strings without malloc)
How can I understand when I need to free up memory, and when not?(password = conn->pgpass; with malloc)

Originally posted by @listepo in #34452 (comment)

@dcharkes can you help me? thanks in advance

@dcharkes
Copy link
Contributor

dcharkes commented Nov 2, 2020

If you'd remove

	if (password == NULL)
		password = "";

then you could just check in dart if the pointer equals nullptr and only free if it does not.

Note that is perfectly possible to free "" if it is actually allocated in the program. "" will still be 1 byte long, and contain the termination byte as first byte, which still needs to be freed when allocated. However, in your code the "" will be a constant which is part of your compiled program and cannot be freed at runtime.

@dcharkes dcharkes closed this as completed Nov 2, 2020
@dcharkes
Copy link
Contributor

dcharkes commented Nov 2, 2020

I completely missed that this is the Postgres API (thanks @mraleph for pointing that out)!

When using an existing library, the existing library has a policy about its resource management. The postgres documentation on the function does not mention resource management at all: https://www.postgresql.org/docs/12/libpq-status.html. However, given that the implementation directly returns conn->connhost[conn->whichhost].password without any copying, freeing that value would likely lead to undefined behavior as well. So I'd infer from this that the callee never has to free a string returned from this function.

@listepo
Copy link
Author

listepo commented Nov 2, 2020

@dcharkes thanks a lot

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