Skip to content

xgetXXbyYY: Improve error handling#729

Merged
hallyn merged 2 commits into
shadow-maint:masterfrom
ferivoz:xx
May 26, 2023
Merged

xgetXXbyYY: Improve error handling#729
hallyn merged 2 commits into
shadow-maint:masterfrom
ferivoz:xx

Conversation

@ferivoz
Copy link
Copy Markdown
Contributor

@ferivoz ferivoz commented May 15, 2023

  • Treat another out of memory condition and fail with exit(13) consistently within the file.
  • Avoid duplicated code in error handling

ferivoz added 2 commits May 15, 2023 12:00
A failure of DUP_FUNCTION is already handled for non-reentrant
function wrapper. Perform the check for reentrant version as well.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
The error handling is performed after the loop. By just calling break it
is possible to reuse the error handling if status is not ERANGE.

Signed-off-by: Samanta Navarro <ferivoz@riseup.net>
@hallyn
Copy link
Copy Markdown
Member

hallyn commented May 22, 2023

Hi,

what do you think about this instead? :

diff --git a/libmisc/xgetXXbyYY.c b/libmisc/xgetXXbyYY.c
index e1bf70f6..ced4fdc5 100644
--- a/libmisc/xgetXXbyYY.c
+++ b/libmisc/xgetXXbyYY.c
@@ -47,7 +47,7 @@
 /*@null@*/ /*@only@*/LOOKUP_TYPE *XFUNCTION_NAME (ARG_TYPE ARG_NAME)
 {
 #if HAVE_FUNCTION_R
-	LOOKUP_TYPE *result=NULL;
+	LOOKUP_TYPE *result=NULL, ret_result=NULL;
 	char *buffer=NULL;
 	/* we have to start with something */
 	size_t length = 0x100;
@@ -68,17 +68,12 @@
 		if ((0 == status) && (resbuf == result)) {
 			/* Build a result structure that can be freed by
 			 * the shadow *_free functions. */
-			LOOKUP_TYPE *ret_result = DUP_FUNCTION(result);
-			free(buffer);
-			free(result);
-			return ret_result;
+			ret_result = DUP_FUNCTION(result);
+			break;
 		}
 
-		if (ERANGE != status) {
-			free (buffer);
-			free (result);
-			return NULL;
-		}
+		if (ERANGE != status)
+			break;
 
 		if (length <= ((size_t)-1 / 4)) {
 			length *= 4;
@@ -91,7 +86,7 @@
 
 	free(buffer);
 	free(result);
-	return NULL;
+	return ret_result;
 
 #else /* !HAVE_FUNCTION_R */
 

@hallyn
Copy link
Copy Markdown
Member

hallyn commented May 22, 2023

(The reason for my suggestion is that IMO it simplifies the success and failure exit cases)

@ferivoz
Copy link
Copy Markdown
Contributor Author

ferivoz commented May 23, 2023

In general I would agree but one commit is about unifying the error handling between reentrant and non-reentrant versions (the else block of the preprocessor if).

If we use your code, we should do the same in the else block, i.e. no exit if memory exhaustion occurred.

Do you want me to modify that code as well? Or do you prefer the exit version in this pull request?

@hallyn
Copy link
Copy Markdown
Member

hallyn commented May 23, 2023

In general I would agree but one commit is about unifying the error handling between reentrant and non-reentrant versions (the else block of the preprocessor if).

If we use your code, we should do the same in the else block, i.e. no exit if memory exhaustion occurred.

Do you want me to modify that code as well? Or do you prefer the exit version in this pull request?

yeah, you're right, if we run out of memory we should exit, no sense going on after that.

@hallyn
Copy link
Copy Markdown
Member

hallyn commented May 23, 2023

Jinkey, the codeql job is pretty fragile

@hallyn hallyn self-requested a review May 23, 2023 22:36
@hallyn hallyn merged commit dcc9065 into shadow-maint:master May 26, 2023
Comment thread libmisc/xgetXXbyYY.c
Comment on lines 71 to +77
LOOKUP_TYPE *ret_result = DUP_FUNCTION(result);
if (NULL == result) {
fprintf (log_get_logfd(),
_("%s: out of memory\n"),
"x" STRINGIZE(FUNCTION_NAME));
exit (13);
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a typo in the NULL check. You probably meant ret_result == NULL, right?

alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request May 27, 2023
It seems obvious that it was a typo.

Link: <shadow-maint#729 (comment)>
Fixes: e73a219 ("xgetXXbyYY: Handle DUP_FUNCTION failure")
Cc: Samanta Navarro <ferivoz@riseup.net>
Cc: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
hallyn pushed a commit that referenced this pull request May 30, 2023
It seems obvious that it was a typo.

Link: <#729 (comment)>
Fixes: e73a219 ("xgetXXbyYY: Handle DUP_FUNCTION failure")
Cc: Samanta Navarro <ferivoz@riseup.net>
Cc: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
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.

3 participants