Skip to contentSkip to conversation

xfstests: fix _BSD_SOURCE complier warning

PATCH New Ross Zwisler 1 participant 3 messages Dec 7, 2017 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 xfstests: fix _BSD_SOURCE complier warning

When compiling xfstests with either gcc 6.4.1 or 7.2.1 I see the following warning:

In file included from /usr/include/stdio.h:27:0,

                 from fssum.c:25:
/usr/include/features.h:148:3: warning: #warning "_BSD_SOURCE and _SVID_SOURCE
are deprecated, use _DEFAULT_SOURCE" [-Wcpp]
 # warning "_BSD_SOURCE and _SVID_SOURCE are deprecated, use _DEFAULT_SOURCE"
   ^~~~~~~

The feature_test_macros(7) man page says this about _BSD_SOURCE:

Since glibc 2.20, this macro is deprecated. It now has the same effect as defining _DEFAULT_SOURCE, but generates a compile-time warning (unless _DEFAULT_SOURCE is also defined). Use _DEFAULT_SOURCE instead. To allow code that requires _BSD_SOURCE in glibc 2.19 and earlier and _DEFAULT_SOURCE in glibc 2.20 and later to compile without warnings, define both _BSD_SOURCE and _DEFAULT_SOURCE.

Keep backwards compatibility with older code by defining both _BSD_SOURCE and _DEFAULT_SOURCE.

---
 src/fssum.c | 1 +
 1 file changed, 1 insertion(+)
src/fssum.c +1 -0
index 04052cdc..13111d6f 100644
@@ -17,6 +17,7 @@
  */
 #ifdef __linux__
 #define _BSD_SOURCE
+#define _DEFAULT_SOURCE
 #define _LARGEFILE64_SOURCE
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE
Signature · 1 line
2.14.3
#1 lore raw

PATCH 2/3 xfstests: fix TagName overflow issues

The 'TagName' string is defined to be 40 characters in length, but in three places we write into it with a format of "(%.39s)". This can result in a string of up to 42 characters, the 39 character user string plus "()\0". This overflows TagName, as we see in the new complier warnings from gcc 7.2.1:

iogen.c:1277:6: note: 'sprintf' output between 3 and 42 bytes into a destination of size 40
sprintf( TagName, "(%.39s)", optarg ); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix this by limiting the user string to 37 characters.

---
 ltp/doio.c      | 2 +-
 ltp/growfiles.c | 2 +-
 ltp/iogen.c     | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
ltp/doio.c +1 -1
index dcb3efd8..b9377187 100644
@@ -3184,7 +3184,7 @@ char	*opts;
 			break;
 
 		case 'N':
-			sprintf( TagName, "(%.39s)", optarg );
+			sprintf( TagName, "(%.37s)", optarg );
 			break;
 
 		case 'n':
ltp/growfiles.c +1 -1
index fb91761f..ad1ecdf5 100644
@@ -794,7 +794,7 @@ extern int Forker_npids;	/* num of forked pid, defined in forker.c */
 			break;
 
 		case 'W':
-			sprintf( TagName, "(%.39s)", optarg );
+			sprintf( TagName, "(%.37s)", optarg );
 			break;
 
 		case 'y':
ltp/iogen.c +1 -1
index 1eaab1f0..8fc339e5 100644
@@ -1274,7 +1274,7 @@ char	*opts;
 	    break;
 
 	case 'N':
-	    sprintf( TagName, "(%.39s)", optarg );
+	    sprintf( TagName, "(%.37s)", optarg );
 	    break;
 
 	case 'o':
Signature · 1 line
2.14.3
#2 lore raw

PATCH 3/3 xfstests: fix Wlog_Error_String overflow issues

In reply to Ross Zwisler

The 'Wlog_Error_String' string is defined to be 256 bytes in length, but in two places we write into it with a format that contains a string (wfile->w_file) that has length 1024. This can overflow Wlog_Error_String, as we see in the new compiler warnings from gcc 7.2.1:

write_log.c:124:37: warning: ‘%s’ directive writing up to 1023 bytes into a region of size 224 [-Wformat-overflow=]
"Could not open write_log - open(%s, %#o, %#o) failed: %s\n",
^~

Fix this by increasing the length of Wlog_Error_String to 1280 characters (1024 for wfile->w_file plus 256 for the rest of the format string), and by using snprintf() instead of sprintf() so we are sure we don't overflow.

---
 lib/write_log.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
lib/write_log.c +4 -3
index 8c921fc5..3bfa8c7b 100644
@@ -73,7 +73,8 @@
 /*#define PATH_MAX pathconf("/", _PC_PATH_MAX)*/
 #endif
 
-char	Wlog_Error_String[256];
+#define ERROR_STRING_LEN 1280
+char	Wlog_Error_String[ERROR_STRING_LEN];
 
 #if __STDC__
 static int	wlog_rec_pack(struct wlog_rec *wrec, char *buf, int flag);
@@ -120,7 +121,7 @@ int			mode;
 	umask(omask);
 
 	if (wfile->w_afd == -1) {
-		sprintf(Wlog_Error_String,
+		snprintf(Wlog_Error_String, ERROR_STRING_LEN,
 			"Could not open write_log - open(%s, %#o, %#o) failed:  %s\n",
 			wfile->w_file, oflags, mode, strerror(errno));
 		return -1;
@@ -132,7 +133,7 @@ int			mode;
 
 	oflags = O_RDWR;
 	if ((wfile->w_rfd = open(wfile->w_file, oflags)) == -1) {
-		sprintf(Wlog_Error_String,
+		snprintf(Wlog_Error_String, ERROR_STRING_LEN,
 			"Could not open write log - open(%s, %#o) failed:  %s\n",
 			wfile->w_file, oflags, strerror(errno));
 		close(wfile->w_afd);
Signature · 1 line
2.14.3