mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] strftime trailing %
@ 2022-12-19 18:06 Rob Landley
  2022-12-19 18:50 ` Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: Rob Landley @ 2022-12-19 18:06 UTC (permalink / raw)
  To: musl

In glibc and bionic a trailing % in strftime() acts like printf, I.E. it's a
literal "%". But in musl, it's an error that returns length 0. Test program:

#include <stdio.h>
#include <time.h>

int main(int argc, char *argv[])
{
  char buf[256];
  struct tm tm = {0};

  strftime(buf, sizeof(buf), "hello %", &tm);
  return printf("%s\n", buf);
}


The fix looks simple enough, although I haven't built a toolchain with it yet:

--- a/src/time/strftime.c
+++ b/src/time/strftime.c
@@ -225,7 +225,7 @@ size_t __strftime_l(char *restrict s, size_t n, const char
*restrict f, const st
 			s[l] = 0;
 			return l;
		}
-		if (*f != '%') {
+		if (*f != '%' || !f[1]) {
 			s[l++] = *f;
 			continue;
 		}

This is breaking a toybox test for the "date" command.

Rob

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [musl] strftime trailing %
  2022-12-19 18:06 [musl] strftime trailing % Rob Landley
@ 2022-12-19 18:50 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2022-12-19 18:50 UTC (permalink / raw)
  To: Rob Landley; +Cc: musl

On Mon, Dec 19, 2022 at 12:06:38PM -0600, Rob Landley wrote:
> In glibc and bionic a trailing % in strftime() acts like printf, I.E. it's a
> literal "%". But in musl, it's an error that returns length 0. Test program:
> 
> #include <stdio.h>
> #include <time.h>
> 
> int main(int argc, char *argv[])
> {
>   char buf[256];
>   struct tm tm = {0};
> 
>   strftime(buf, sizeof(buf), "hello %", &tm);
>   return printf("%s\n", buf);
> }
> 
> 
> The fix looks simple enough, although I haven't built a toolchain with it yet:
> 
> --- a/src/time/strftime.c
> +++ b/src/time/strftime.c
> @@ -225,7 +225,7 @@ size_t __strftime_l(char *restrict s, size_t n, const char
> *restrict f, const st
>  			s[l] = 0;
>  			return l;
> 		}
> -		if (*f != '%') {
> +		if (*f != '%' || !f[1]) {
>  			s[l++] = *f;
>  			continue;
>  		}
> 
> This is breaking a toybox test for the "date" command.

Is this supposed to be well-defined, either for the date command or
for strftime? I don't see where it's explicitly covered in any of the
spec so if it's defined or undefined/unspecified would presumably fall
out as a consequence of how conversion specifications and ordinary
characters are defined.

Rich

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-12-19 18:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-19 18:06 [musl] strftime trailing % Rob Landley
2022-12-19 18:50 ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).