mailing list of musl libc
 help / color / mirror / code / Atom feed
* Trouble with sscanf
@ 2012-11-16 15:28 Paul Schutte
  2012-11-16 15:39 ` Szabolcs Nagy
  0 siblings, 1 reply; 5+ messages in thread
From: Paul Schutte @ 2012-11-16 15:28 UTC (permalink / raw)
  To: musl


[-- Attachment #1.1: Type: text/plain, Size: 435 bytes --]

Hi Guys,

I am trying to compile procps-3.2.8. I keep getting floating point
exceptions when trying to run the tools.

I narrowed the problem to what seems to be an issue with sscanf.

glibc output:
3720798 10228 2410878 64601718 356554 1658 33774 0

musl output:
100 0 0 0 0 0 0 0

I hacked sample code together out of their code to illustrate the problem,
which I attach.
I tried to keep it very close to the original.

Regards
Paul

[-- Attachment #1.2: Type: text/html, Size: 625 bytes --]

[-- Attachment #2: scanftest.c --]
[-- Type: text/x-csrc, Size: 1192 bytes --]

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

#define BUFFSIZE (128*1024)
typedef unsigned long long jiff;

int main() {
static char buff[BUFFSIZE]; /* used in the procedures */
static int fd;
jiff cuse;
jiff cice;
jiff csys;
jiff cide;
jiff ciow;
jiff cxxx;
jiff cyyy;
jiff czzz;
unsigned intr;
const char* b=NULL;

buff[BUFFSIZE-1] = 0;  /* ensure null termination in buffer */

if (fd) {
	lseek(fd, 0L, SEEK_SET);
	}else{
	fd = open("/proc/stat", O_RDONLY, 0);
	if(fd == -1) perror("/proc/stat");
}

  read(fd,buff,BUFFSIZE-1);

	intr = 0;
  ciow = 0;  /* not separated out until the 2.5.41 kernel */
  cxxx = 0;  /* not separated out until the 2.6.0-test4 kernel */
  cyyy = 0;  /* not separated out until the 2.6.0-test4 kernel */
  czzz = 0;  /* not separated out until the 2.6.11 kernel */

  b = strstr(buff, "cpu ");
  if (b) { 
	sscanf(b,  "cpu  %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu", &cuse, &cice, &csys, &cide, &ciow, &cxxx, &cyyy, &czzz);
	printf("%llu %llu %llu %llu %llu %llu %llu %llu\n",cuse,cice,csys,cide,ciow,cxxx,cyyy,czzz);
	return 0;
	} else {
	printf("strstr failed\n");
	}
}

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

* Re: Trouble with sscanf
  2012-11-16 15:28 Trouble with sscanf Paul Schutte
@ 2012-11-16 15:39 ` Szabolcs Nagy
  2012-11-16 15:46   ` Paul Schutte
  2012-11-16 15:49   ` Szabolcs Nagy
  0 siblings, 2 replies; 5+ messages in thread
From: Szabolcs Nagy @ 2012-11-16 15:39 UTC (permalink / raw)
  To: musl

* Paul Schutte <sjpschutte@gmail.com> [2012-11-16 17:28:57 +0200]:
> 	sscanf(b,  "cpu  %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu", &cuse, &cice, &csys, &cide, &ciow, &cxxx, &cyyy, &czzz);

there is no L length modifier for ints

L is for long double formatting (%Lf %Lg %Le ...)
i guess musl returns an early matching error
but they don't check for that

rewrite it to use %llu


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

* Re: Trouble with sscanf
  2012-11-16 15:39 ` Szabolcs Nagy
@ 2012-11-16 15:46   ` Paul Schutte
  2012-11-16 15:53     ` John Spencer
  2012-11-16 15:49   ` Szabolcs Nagy
  1 sibling, 1 reply; 5+ messages in thread
From: Paul Schutte @ 2012-11-16 15:46 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 600 bytes --]

Thanks.

BTW. This code that I just showed you is what you run every time you use
top, ps, vmstat on Debian or Ubuntu.

Regards
Paul

On Fri, Nov 16, 2012 at 5:39 PM, Szabolcs Nagy <nsz@port70.net> wrote:

> * Paul Schutte <sjpschutte@gmail.com> [2012-11-16 17:28:57 +0200]:
> >       sscanf(b,  "cpu  %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu", &cuse, &cice,
> &csys, &cide, &ciow, &cxxx, &cyyy, &czzz);
>
> there is no L length modifier for ints
>
> L is for long double formatting (%Lf %Lg %Le ...)
> i guess musl returns an early matching error
> but they don't check for that
>
> rewrite it to use %llu
>

[-- Attachment #2: Type: text/html, Size: 1027 bytes --]

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

* Re: Trouble with sscanf
  2012-11-16 15:39 ` Szabolcs Nagy
  2012-11-16 15:46   ` Paul Schutte
@ 2012-11-16 15:49   ` Szabolcs Nagy
  1 sibling, 0 replies; 5+ messages in thread
From: Szabolcs Nagy @ 2012-11-16 15:49 UTC (permalink / raw)
  To: musl

* Szabolcs Nagy <nsz@port70.net> [2012-11-16 16:39:21 +0100]:
> i guess musl returns an early matching error
> but they don't check for that

sorry there is no matching error

"If a length modifier appears with any conversion specifier other than as specified above, the behavior is undefined."
http://port70.net/~nsz/c/c11/n1570.html#7.21.6.2p11

so "%Lu" format in scanf invokes undefined behaviour


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

* Re: Trouble with sscanf
  2012-11-16 15:46   ` Paul Schutte
@ 2012-11-16 15:53     ` John Spencer
  0 siblings, 0 replies; 5+ messages in thread
From: John Spencer @ 2012-11-16 15:53 UTC (permalink / raw)
  To: musl

On 11/16/2012 04:46 PM, Paul Schutte wrote:
> Thanks.
>
> BTW. This code that I just showed you is what you run every time you use
> top, ps, vmstat on Debian or Ubuntu.

can you send a patch to upstream ? the same bug was recently fixed in alsa:
http://git.alsa-project.org/?p=alsa-lib.git;a=commitdiff;h=1d3f7975f920f47e6a8a324f547da2180e64171a;hp=bb5c49fa4160ec1d819fb03fc8dfb5387dad0522




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

end of thread, other threads:[~2012-11-16 15:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-16 15:28 Trouble with sscanf Paul Schutte
2012-11-16 15:39 ` Szabolcs Nagy
2012-11-16 15:46   ` Paul Schutte
2012-11-16 15:53     ` John Spencer
2012-11-16 15:49   ` Szabolcs Nagy

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).