Skip to contentSkip to conversation

junction: export-cache: cast to a type with a known size to ensure sprintf works

PATCH In review Salvatore Bonaccorso 2 participants 2 messages May 2, 2024 – May 10, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 junction: export-cache: cast to a type with a known size to ensure sprintf works

As reported in Debian, with the 64bit time_t transition for the armel and armhf architecture, it was found that nfs-utils fails to compile with:

libtool: compile: gcc -DHAVE_CONFIG_H -I. -I../../support/include -I/usr/include/tirpc -I/usr/include/libxml2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64 -Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -pipe -Wall -Wextra -Werror=strict-prototypes -Werror=missing-prototypes -Werror=missing-declarations -Werror=format=2 -Werror=undef -Werror=missing-include-dirs -Werror=strict-aliasing=2 -Werror=init-self -Werror=implicit-function-declaration -Werror=return-type -Werror=switch -Werror=overflow -Werror=parentheses -Werror=aggregate-return -Werror=unused-result -fno-strict-aliasing -Werror=format-overflow=2 -Werror=int-conversion -Werror=incompatible-pointer-types -Werror=misleading-indentation -Wno-cast-function-type -g -O2 -Werror=implicit-function-declaration -ffile-prefix-map=/<<PKGBUILDDIR>>=. -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -c xml.c -fPIC -DPIC -o .libs/xml.o export-cache.c: In function ‘junction_flush_exports_cache’:
export-cache.c:110:51: error: format ‘%ld’ expects argument of type ‘long int’, but argument 4 has type ‘time_t’ {aka ‘long long int’} [-Werror=format=]
110 | snprintf(flushtime, sizeof(flushtime), "%ld\n", now);
| ~~^ ~~~
| | |
| | time_t {aka long long int}
| long int
| %lld

time_t is not guaranteed to be 64-bit, so it must be coerced into the expected type for printf. Cast it to long long.

---
 support/junction/export-cache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
support/junction/export-cache.c +1 -1
index 4e578c9b37b1..00187c019d60 100644
@@ -107,7 +107,7 @@ junction_flush_exports_cache(void)
 		xlog(D_GENERAL, "%s: time(3) failed", __func__);
 		return FEDFS_ERR_SVRFAULT;
 	}
-	snprintf(flushtime, sizeof(flushtime), "%ld\n", now);
+	snprintf(flushtime, sizeof(flushtime), "%lld\n", (long long)now);
 
 	for (i = 0; junction_proc_files[i] != NULL; i++) {
 		retval = junction_write_time(junction_proc_files[i], flushtime);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 17 lines · Salvatore Bonaccorso

As reported in Debian, with the 64bit time_t transition for the armel and armhf architecture, it was found that nfs-utils fails to compile with:

libtool: compile: gcc -DHAVE_CONFIG_H -I. -I../../support/include -I/usr/include/tirpc -I/usr/include/libxml2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64 -Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -pipe -Wall -Wextra -Werror=strict-prototypes -Werror=missing-prototypes -Werror=missing-declarations -Werror=format=2 -Werror=undef -Werror=missing-include-dirs -Werror=strict-aliasing=2 -Werror=init-self -Werror=implicit-function-declaration -Werror=return-type -Werror=switch -Werror=overflow -Werror=parentheses -Werror=aggregate-return -Werror=unused-result -fno-strict-aliasing -Werror=format-overflow=2 -Werror=int-conversion -Werror=incompatible-pointer-types -Werror=misleading-indentation -Wno-cast-function-type -g -O2 -Werror=implicit-function-declaration -ffile-prefix-map=/<<PKGBUILDDIR>>=. -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -c xml.c -fPIC -DPIC -o .libs/xml.o export-cache.c: In function ‘junction_flush_exports_cache’:
export-cache.c:110:51: error: format ‘%ld’ expects argument of type ‘long int’, but argument 4 has type ‘time_t’ {aka ‘long long int’} [-Werror=format=]
110 | snprintf(flushtime, sizeof(flushtime), "%ld\n", now);
| ~~^ ~~~
| | |
| | time_t {aka long long int}
| long int
| %lld

time_t is not guaranteed to be 64-bit, so it must be coerced into the expected type for printf. Cast it to long long.

Quoted 7 lines

Committed...

steved.

Quoted 17 lines
---
  support/junction/export-cache.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
support/junction/export-cache.c +1 -1
index 4e578c9b37b1..00187c019d60 100644
@@ -107,7 +107,7 @@ junction_flush_exports_cache(void)
  		xlog(D_GENERAL, "%s: time(3) failed", __func__);
  		return FEDFS_ERR_SVRFAULT;
  	}
-	snprintf(flushtime, sizeof(flushtime), "%ld\n", now);
+	snprintf(flushtime, sizeof(flushtime), "%lld\n", (long long)now);
 
  	for (i = 0; junction_proc_files[i] != NULL; i++) {
  		retval = junction_write_time(junction_proc_files[i], flushtime);