Skip to contentSkip to conversation

lib/tst_tmpdir: tst_get_tmpdir() add error handing

PATCH New zhanglianjie 3 participants 5 messages Aug 6, 2021 – Aug 9, 2021 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/tst_tmpdir: tst_get_tmpdir() add error handing

lib/tst_tmpdir.c +7 -1
index 0c39eb89f..f006e4893 100644
@@ -108,12 +108,18 @@ int tst_tmpdir_created(void)
 
 char *tst_get_tmpdir(void)
 {
+	char *ret = NULL;
+
 	if (TESTDIR == NULL) {
 		tst_brkm(TBROK, NULL, "you must call tst_tmpdir() first");
 		return NULL;
 	}
 
-	return strdup(TESTDIR);
+	ret = strdup(TESTDIR);
+	if (!ret)
+		tst_brkm(TBROK, NULL, "strdup() failed");
+
+	return ret;
 }
 
 const char *tst_get_startwd(void)

--
2.20.1

#1 lore raw

PATCH 1/1 lib/tst_tmpdir: tst_get_tmpdir() add error handing

Hi

Quoted 9 lines · zhanglianjie
lib/tst_tmpdir.c +0 -0
index 0c39eb89f..f006e4893 100644
@@ -108,12 +108,18 @@ int tst_tmpdir_created(void)
 
  char *tst_get_tmpdir(void)

lib/tst_tmpdir.c int tst_tmpdir_created(void) · line 118

Quoted 10 lines
lib/tst_tmpdir.c +3 -1
  {
+	char *ret = NULL;
+
  	if (TESTDIR == NULL) {
  		tst_brkm(TBROK, NULL, "you must call tst_tmpdir() first");
  		return NULL;
  	}
 
-	return strdup(TESTDIR);
+	ret = strdup(TESTDIR);

Is a failing strdup here really a thing? The only reason strdup should be able to fail is with ENOMEM.
The only way tst_brkm will work, if strdup fails here is, if TESTDIR is an extremely huge string (the NULL case is already handled above).

Quoted 11 lines
lib/tst_tmpdir.c +4 -1
+	if (!ret)
+		tst_brkm(TBROK, NULL, "strdup() failed");
+
+	return ret;
  }
 
  const char *tst_get_startwd(void)
--

2.20.1

Joerg

#2 lore raw

PATCH 1/1 lib/tst_tmpdir: tst_get_tmpdir() add error handing

Hi,

Quoted 18 lines · zhanglianjie
Quoted 17 lines
lib/tst_tmpdir.c +2 -0
index 0c39eb89f..f006e4893 100644
@@ -108,12 +108,18 @@ int tst_tmpdir_created(void)
 
 ? char *tst_get_tmpdir(void)
 ? {
+??? char *ret = NULL;
+
 ????? if (TESTDIR == NULL) {
 ????????? tst_brkm(TBROK, NULL, "you must call tst_tmpdir() first");
 ????????? return NULL;
 ????? }
 
Quoted 6 lines

lib/tst_tmpdir.c int tst_tmpdir_created(void) · line 118

Quoted 2 lines
lib/tst_tmpdir.c +1 -1
-??? return strdup(TESTDIR);
+??? ret = strdup(TESTDIR);

Is a failing strdup here really a thing? The only reason strdup should be able to fail is with ENOMEM.
The only way tst_brkm will work, if strdup fails here is, if TESTDIR is an extremely huge string (the NULL case is already handled above).

If don?t consider a very huge string, you don?t need to modify it here. This is a habitual judgment.
Thank you for your review.

Quoted 13 lines
Quoted 11 lines
lib/tst_tmpdir.c +4 -0
+??? if (!ret)
+??????? tst_brkm(TBROK, NULL, "strdup() failed");
+
+??? return ret;
 ? }
 
 ? const char *tst_get_startwd(void)
Signature · 1 line
2.20.1

Joerg

Signature · 2 lines
Regards,
Zhang Lianjie
#3 lore raw

PATCH 1/1 lib/tst_tmpdir: tst_get_tmpdir() add error handing

In reply to Joerg Vehlow

Hi!

Quoted 6 lines

lib/tst_tmpdir.c int tst_tmpdir_created(void) · line 118

Quoted 2 lines
lib/tst_tmpdir.c +1 -1
-	return strdup(TESTDIR);
+	ret = strdup(TESTDIR);

Is a failing strdup here really a thing? The only reason strdup should be able to fail is with ENOMEM.
The only way tst_brkm will work, if strdup fails here is, if TESTDIR is an extremely huge string (the NULL case is already handled above).

It's unlikely, but it may happen if:

* Someone runs memeater on the baground along with LTP
* And kernel is set not to overcommit

And in a case of the test lirary I would rather see it written so that we check everything, even the unlikely errors.

Signature · 2 lines
Cyril Hrubis
[email protected]