Skip to contentSkip to conversation

Question about strncpy_2-1

LTP Superseded Xiao Yang 2 participants 9 messages Dec 11, 2018 – Dec 14, 2018

lore
Share & tools
lore

Xiao Yang

author
#0 lore raw

Hi all,

With old gcc version(e.g. gcc-4.4), compiling strncpy_2-1 gets the following error:
----------------------------------------------------------------------------------
../../../conformance/interfaces/strncpy/1-1.c: In function ‘main’:
../../../conformance/interfaces/strncpy/1-1.c:64: error: #pragma GCC diagnostic not allowed inside functions ../../../conformance/interfaces/strncpy/1-1.c:65: error: #pragma GCC diagnostic not allowed inside functions ../../../conformance/interfaces/strncpy/1-1.c:67: error: #pragma GCC diagnostic not allowed inside functions conformance/interfaces/strncpy/1-1 compile FAILED; SKIPPING
----------------------------------------------------------------------------------

It seems that push/pop is supported and pragma diagnostic is allowed inside functions since commit 0955be6 in GCC.

It is clear for me to know tha fact that current gcc version doesn't support above pragma diagnostic code, so anyone can tell me if we should keep the error?

Best Regards,
Xiao Yang

author
#1 lore raw
Quoted 11 lines · Xiao Yang

Hi all,

With old gcc version(e.g. gcc-4.4), compiling strncpy_2-1 gets the following error:
----------------------------------------------------------------------------------
../../../conformance/interfaces/strncpy/1-1.c: In function ‘main’:
../../../conformance/interfaces/strncpy/1-1.c:64: error: #pragma GCC diagnostic not allowed inside functions ../../../conformance/interfaces/strncpy/1-1.c:65: error: #pragma GCC diagnostic not allowed inside functions ../../../conformance/interfaces/strncpy/1-1.c:67: error: #pragma GCC diagnostic not allowed inside functions conformance/interfaces/strncpy/1-1 compile FAILED; SKIPPING
----------------------------------------------------------------------------------

Quoted 5 lines

It seems that push/pop is supported and pragma diagnostic is allowed inside functions since commit 0955be6 in GCC.

It is clear for me to know tha fact that current gcc version doesn't support above pragma diagnostic code, so anyone can tell me if we should keep the error?

Hi all,

In addition, this compiler error doesn't break the whole openposix compilation.

Best Regards,
Xiao Yang

Quoted 6 lines

Best Regards,
Xiao Yang

#2 lore raw

Hi!

Quoted 1 line

In addition, this compiler error doesn't break the whole openposix compilation.

That is the desing of the openposix build system, compilation failures are not fatal.

Signature · 2 lines
Cyril Hrubis
[email protected]
#3 lore raw
In reply to Xiao Yang

Hi!

Quoted 11 lines

With old gcc version(e.g. gcc-4.4), compiling strncpy_2-1 gets the following error:
----------------------------------------------------------------------------------
../../../conformance/interfaces/strncpy/1-1.c: In function ???main???:
../../../conformance/interfaces/strncpy/1-1.c:64: error: #pragma GCC diagnostic not allowed inside functions ../../../conformance/interfaces/strncpy/1-1.c:65: error: #pragma GCC diagnostic not allowed inside functions ../../../conformance/interfaces/strncpy/1-1.c:67: error: #pragma GCC diagnostic not allowed inside functions conformance/interfaces/strncpy/1-1 compile FAILED; SKIPPING
----------------------------------------------------------------------------------

It seems that push/pop is supported and pragma diagnostic is allowed inside functions since commit 0955be6 in GCC.

Looks like an attempt to silence warnings, but a lousy one that would break on anything but new enough gcc, let's get rid of that.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#4 lore raw

PATCH 1/1 openposix/strncpy1-1: Fix compiler errors/warnings

pragma diagnostic can support push/pop and be allowed inside functions by commit 0955be65 on gcc-4.6, and it can support -Wstringop-truncation by commit d8aad78 on gcc-8.1. We try to avoid some compiler errors or warnings by just using pragma diagnostic for test on newer gcc(i.e. >= gcc-8.1) because compiling and running test don't get any failure after removing pragma diagnostic on older gcc.

For example:
1)compiling test gets the following error on gcc-4.4:
------------------------------------------------------
error: #pragma GCC diagnostic not allowed inside functions
------------------------------------------------------
2)compiling test gets the following warning on gcc-4.8:
-----------------------------------------------------
warning: unknown option after ‘#pragma GCC diagnostic’ kind [-Wpragmas]
------------------------------------------------------

---
 .../conformance/interfaces/strncpy/1-1.c           |   10 +++++++---
 testcases/open_posix_testsuite/include/posixtest.h |    2 ++
 2 files changed, 9 insertions(+), 3 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/strncpy/1-1.c +7 -3
index 396bd60..7006b5f 100644
@@ -61,10 +61,14 @@ int main(void)
         sample_str_1 = random_string(i);
         num_bytes = rand() % i;
 
-        #pragma GCC diagnostic push
-        #pragma GCC diagnostic ignored "-Wstringop-truncation"
+	#if GCC_VERSION >= 80100
+	# pragma GCC diagnostic push
+	# pragma GCC diagnostic ignored "-Wstringop-truncation"
+	#endif
         ret_str = strncpy(sample_str_2, sample_str_1, num_bytes);
-        #pragma GCC diagnostic pop
+	#if GCC_VERSION >= 80100
+	# pragma GCC diagnostic pop
+	#endif
 
         sample_str_2[num_bytes] = '\0';
         sample_str_1[num_bytes] = '\0';
testcases/open_posix_testsuite/include/posixtest.h +2 -0
index dd1a9b2..8859804 100644
@@ -47,3 +47,5 @@
 #define LTP_ATTRIBUTE_NORETURN		__attribute__((noreturn))
 #define LTP_ATTRIBUTE_UNUSED		__attribute__((unused))
 #define LTP_ATTRIBUTE_UNUSED_RESULT	__attribute__((warn_unused_result))
+
+#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
Signature · 1 line
1.7.1
author
#6 lore raw

PATCH v2 1/1 openposix/strncpy1-1: Fix compiler errors/warnings

In reply to Cyril Hrubis

pragma diagnostic can support push/pop and be allowed inside functions by commit 0955be65 on gcc-4.6, and it can support -Wstringop-truncation by commit d8aad78 on gcc-8.1. We try to avoid some compiler errors or warnings by removing pragma diagnostic for test directly, because we don't get any failure after removing it on old/new gcc.

For example:
1)compiling test gets the following error on gcc-4.4:
------------------------------------------------------
error: #pragma GCC diagnostic not allowed inside functions
------------------------------------------------------
2)compiling test gets the following warning on gcc-4.8:
-----------------------------------------------------
warning: unknown option after ‘#pragma GCC diagnostic’ kind [-Wpragmas]
------------------------------------------------------

---
 testcases/open_posix_testsuite/conformance/interfaces/strncpy/1-1.c | 3 ---
 1 file changed, 3 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/strncpy/1-1.c +0 -3
index 396bd60..b67d09d 100644
@@ -61,10 +61,7 @@ int main(void)
         sample_str_1 = random_string(i);
         num_bytes = rand() % i;
 
-        #pragma GCC diagnostic push
-        #pragma GCC diagnostic ignored "-Wstringop-truncation"
         ret_str = strncpy(sample_str_2, sample_str_1, num_bytes);
-        #pragma GCC diagnostic pop
 
         sample_str_2[num_bytes] = '\0';
         sample_str_1[num_bytes] = '\0';
Signature · 1 line
1.8.3.1
#5 lore raw

PATCH 1/1 openposix/strncpy1-1: Fix compiler errors/warnings

In reply to Xiao Yang

Hi!

Quoted 15 lines
---
 .../conformance/interfaces/strncpy/1-1.c           |   10 +++++++---
 testcases/open_posix_testsuite/include/posixtest.h |    2 ++
 2 files changed, 9 insertions(+), 3 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/strncpy/1-1.c +0 -1
index 396bd60..7006b5f 100644
@@ -61,10 +61,14 @@ int main(void)
         sample_str_1 = random_string(i);
         num_bytes = rand() % i;
 
-        #pragma GCC diagnostic push

testcases/open_posix_testsuite/conformance/interfaces/strncpy/1-1.c int main(void) · line 71

Quoted 10 lines
testcases/open_posix_testsuite/conformance/interfaces/strncpy/1-1.c +7 -2
-        #pragma GCC diagnostic ignored "-Wstringop-truncation"
+	#if GCC_VERSION >= 80100
+	# pragma GCC diagnostic push
+	# pragma GCC diagnostic ignored "-Wstringop-truncation"
+	#endif
         ret_str = strncpy(sample_str_2, sample_str_1, num_bytes);
-        #pragma GCC diagnostic pop
+	#if GCC_VERSION >= 80100
+	# pragma GCC diagnostic pop
+	#endif

That looks even more silly than the original. I do not think that it's reasonable to mess up the code that bad only to surpress a single warning.

So what abour removing the pragmas entirely?

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#8 lore raw

PATCH 1/1 openposix/strncpy1-1: Fix compiler errors/warnings

Quoted 30 lines · Cyril Hrubis

Hi!

Quoted 15 lines
---
  .../conformance/interfaces/strncpy/1-1.c           |   10 +++++++---
  testcases/open_posix_testsuite/include/posixtest.h |    2 ++
  2 files changed, 9 insertions(+), 3 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/strncpy/1-1.c +0 -1
index 396bd60..7006b5f 100644
@@ -61,10 +61,14 @@ int main(void)
          sample_str_1 = random_string(i);
          num_bytes = rand() % i;
 
-        #pragma GCC diagnostic push

testcases/open_posix_testsuite/conformance/interfaces/strncpy/1-1.c int main(void) · line 71

Quoted 10 lines
testcases/open_posix_testsuite/conformance/interfaces/strncpy/1-1.c +7 -2
-        #pragma GCC diagnostic ignored "-Wstringop-truncation"
+	#if GCC_VERSION>= 80100
+	# pragma GCC diagnostic push
+	# pragma GCC diagnostic ignored "-Wstringop-truncation"
+	#endif
          ret_str = strncpy(sample_str_2, sample_str_1, num_bytes);
-        #pragma GCC diagnostic pop
+	#if GCC_VERSION>= 80100
+	# pragma GCC diagnostic pop
+	#endif

That looks even more silly than the original. I do not think that it's reasonable to mess up the code that bad only to surpress a single warning.

Quoted 2 lines

So what abour removing the pragmas entirely?

Hi Cyril,

I didn't get any failure after removing the pragmas on older/newer gcc, so i think

we can remove it directly.

Best Regards,
Xiao Yang