static int do_test (void) { void *p, *q; int save; errno = 0; DIAG_PUSH_NEEDS_COMMENT; #if __GNUC_PREREQ (7, 0) /* GCC 7 warns about too-large allocations; here we want to test that they fail. */ DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than="); #endif p = malloc (-1); DIAG_POP_NEEDS_COMMENT; save = errno; if (p != NULL) merror ("malloc (-1) succeeded."); if (p == NULL && save != ENOMEM) merror ("errno is not set correctly"); p = malloc (10); if (p == NULL) merror ("malloc (10) failed."); /* realloc (p, 0) == free (p). */ p = realloc (p, 0); if (p != NULL) merror ("realloc (p, 0) failed."); p = malloc (0); if (p == NULL) merror ("malloc (0) failed."); p = realloc (p, 0); if (p != NULL) merror ("realloc (p, 0) failed."); p = malloc (513 * 1024); if (p == NULL) merror ("malloc (513K) failed."); DIAG_PUSH_NEEDS_COMMENT; #if __GNUC_PREREQ (7, 0) /* GCC 7 warns about too-large allocations; here we want to test that they fail. */ DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than="); #endif q = malloc (-512 * 1024); DIAG_POP_NEEDS_COMMENT; if (q != NULL) merror ("malloc (-512K) succeeded."); free (p); return errors != 0; }
int main (int argc, char *argv[]) { long long int n; int ret; n = -1; ret = sscanf ("1000", "%lld", &n); printf ("%%lld: ret: %d, n: %Ld\n", ret, n); if (ret != 1 || n != 1000L) abort (); n = -2; /* We are testing a corner case of the scanf format string here. */ DIAG_PUSH_NEEDS_COMMENT; DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat"); DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat-extra-args"); ret = sscanf ("1000", "%llld", &n); DIAG_POP_NEEDS_COMMENT; printf ("%%llld: ret: %d, n: %Ld\n", ret, n); if (ret > 0 || n >= 0L) abort (); return 0; }
static mach_port_t * interrupted_reply_port_location (thread_t thread, struct machine_thread_all_state *thread_state, int sigthread) { mach_port_t *portloc = &THREAD_TCB(thread, thread_state)->reply_port; if (sigthread && _hurdsig_catch_memory_fault (portloc)) /* Faulted trying to read the TCB. */ return NULL; DIAG_PUSH_NEEDS_COMMENT; /* GCC 6 and before seem to be confused by the setjmp call inside _hurdsig_catch_memory_fault and think that we may be returning a second time to here with portloc uninitialized (but we never do). */ DIAG_IGNORE_NEEDS_COMMENT (6, "-Wmaybe-uninitialized"); /* Fault now if this pointer is bogus. */ *(volatile mach_port_t *) portloc = *portloc; DIAG_POP_NEEDS_COMMENT; if (sigthread) _hurdsig_end_catch_fault (); return portloc; }
static int strncmp_max (const char *left, const char *right) { DIAG_PUSH_NEEDS_COMMENT; #if __GNUC_PREREQ (7, 0) /* GCC 9 warns about the size passed to strncmp being larger than PTRDIFF_MAX; the use of SIZE_MAX is deliberate here. */ DIAG_IGNORE_NEEDS_COMMENT (9, "-Wstringop-overflow="); #endif return strncmp (left, right, SIZE_MAX); DIAG_POP_NEEDS_COMMENT; }
static int t2 (void) { int result = 0; int n; long N; int retval; #define SCAN(INPUT, FORMAT, VAR, EXP_RES, EXP_VAL) \ VAR = -1; \ retval = sscanf (INPUT, FORMAT, &VAR); \ printf ("sscanf (\"%s\", \"%s\", &x) => %d, x = %ld\n", \ INPUT, FORMAT, retval, (long int) VAR); \ result |= retval != EXP_RES || VAR != EXP_VAL /* This function is testing corner cases of the scanf format string, so they do not all conform to -Wformat's expectations. */ DIAG_PUSH_NEEDS_COMMENT; DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat"); DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat-extra-args"); SCAN ("12345", "%ld", N, 1, 12345); SCAN ("12345", "%llllld", N, 0, -1); SCAN ("12345", "%LLLLLd", N, 0, -1); SCAN ("test ", "%*s%n", n, 0, 4); SCAN ("test ", "%2*s%n", n, 0, -1); SCAN ("12 ", "%l2d", n, 0, -1); SCAN ("12 ", "%2ld", N, 1, 12); n = -1; N = -1; retval = sscanf ("1 1", "%d %Z", &n, &N); printf ("sscanf (\"1 1\", \"%%d %%Z\", &n, &N) => %d, n = %d, N = %ld\n", \ retval, n, N); \ result |= retval != 1 || n != 1 || N != -1; DIAG_POP_NEEDS_COMMENT; return result; }
int main(int arc, char *argv[]) { int res; unsigned int val; FILE *fp = fopen ("/dev/null", "r"); val = 0; res = fscanf(fp, "%n", &val); printf("Result of fscanf %%n = %d\n", res); printf("Scanned format = %d\n", val); /* We're testing exactly the case the warning is for. */ DIAG_PUSH_NEEDS_COMMENT; DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat-zero-length"); res = fscanf(fp, ""); DIAG_POP_NEEDS_COMMENT; printf("Result of fscanf \"\" = %d\n", res); if (res != 0) abort (); res = fscanf(fp, "BLURB"); printf("Result of fscanf \"BLURB\" = %d\n", res); if (res >= 0) abort (); fclose (fp); return 0; return 0; }
static int F (void) { char buf[80]; wchar_t wbuf[40]; int result = 0; qnanval = NAN; /* The %f and %F arguments are in fact constants, but GCC is prevented from seeing this (volatile is used) so it cannot tell that the output is not truncated. */ DIAG_PUSH_NEEDS_COMMENT; #if __GNUC_PREREQ (7, 0) DIAG_IGNORE_NEEDS_COMMENT (7.0, "-Wformat-truncation"); #endif snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G", qnanval, qnanval, qnanval, qnanval, qnanval, qnanval, qnanval, qnanval); result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0; printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf); snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G", -qnanval, -qnanval, -qnanval, -qnanval, -qnanval, -qnanval, -qnanval, -qnanval); result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n", buf); snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G", snanval, snanval, snanval, snanval, snanval, snanval, snanval, snanval); result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0; printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf); snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G", msnanval, msnanval, msnanval, msnanval, msnanval, msnanval, msnanval, msnanval); result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n", buf); infval = DBL_MAX * DBL_MAX; snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G", infval, infval, infval, infval, infval, infval, infval, infval); result |= strcmp (buf, "inf INF inf INF inf INF inf INF") != 0; printf ("expected \"inf INF inf INF inf INF inf INF\", got \"%s\"\n", buf); snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G", -infval, -infval, -infval, -infval, -infval, -infval, -infval, -infval); result |= strcmp (buf, "-inf -INF -inf -INF -inf -INF -inf -INF") != 0; printf ("expected \"-inf -INF -inf -INF -inf -INF -inf -INF\", got \"%s\"\n", buf); swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G", qnanval, qnanval, qnanval, qnanval, qnanval, qnanval, qnanval, qnanval); result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0; printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf); swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G", -qnanval, -qnanval, -qnanval, -qnanval, -qnanval, -qnanval, -qnanval, -qnanval); result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n", wbuf); swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G", snanval, snanval, snanval, snanval, snanval, snanval, snanval, snanval); result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0; printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf); swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G", msnanval, msnanval, msnanval, msnanval, msnanval, msnanval, msnanval, msnanval); result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n", wbuf); swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G", infval, infval, infval, infval, infval, infval, infval, infval); result |= wcscmp (wbuf, L"inf INF inf INF inf INF inf INF") != 0; printf ("expected L\"inf INF inf INF inf INF inf INF\", got L\"%S\"\n", wbuf); swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G", -infval, -infval, -infval, -infval, -infval, -infval, -infval, -infval); result |= wcscmp (wbuf, L"-inf -INF -inf -INF -inf -INF -inf -INF") != 0; printf ("expected L\"-inf -INF -inf -INF -inf -INF -inf -INF\", got L\"%S\"\n", wbuf); lqnanval = NAN; snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG", lqnanval, lqnanval, lqnanval, lqnanval, lqnanval, lqnanval, lqnanval, lqnanval); result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0; printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf); snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG", -lqnanval, -lqnanval, -lqnanval, -lqnanval, -lqnanval, -lqnanval, -lqnanval, -lqnanval); result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n", buf); snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG", lsnanval, lsnanval, lsnanval, lsnanval, lsnanval, lsnanval, lsnanval, lsnanval); result |= strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0; printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf); snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG", lmsnanval, lmsnanval, lmsnanval, lmsnanval, lmsnanval, lmsnanval, lmsnanval, lmsnanval); result |= strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n", buf); linfval = LDBL_MAX * LDBL_MAX; snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG", linfval, linfval, linfval, linfval, linfval, linfval, linfval, linfval); result |= strcmp (buf, "inf INF inf INF inf INF inf INF") != 0; printf ("expected \"inf INF inf INF inf INF inf INF\", got \"%s\"\n", buf); snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG", -linfval, -linfval, -linfval, -linfval, -linfval, -linfval, -linfval, -linfval); result |= strcmp (buf, "-inf -INF -inf -INF -inf -INF -inf -INF") != 0; printf ("expected \"-inf -INF -inf -INF -inf -INF -inf -INF\", got \"%s\"\n", buf); swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%La %LA %Le %LE %Lf %LF %Lg %LG", lqnanval, lqnanval, lqnanval, lqnanval, lqnanval, lqnanval, lqnanval, lqnanval); result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0; printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf); swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%La %LA %Le %LE %Lf %LF %Lg %LG", -lqnanval, -lqnanval, -lqnanval, -lqnanval, -lqnanval, -lqnanval, -lqnanval, -lqnanval); result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n", wbuf); swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%La %LA %Le %LE %Lf %LF %Lg %LG", lsnanval, lsnanval, lsnanval, lsnanval, lsnanval, lsnanval, lsnanval, lsnanval); result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0; printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf); swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%La %LA %Le %LE %Lf %LF %Lg %LG", lmsnanval, lmsnanval, lmsnanval, lmsnanval, lmsnanval, lmsnanval, lmsnanval, lmsnanval); result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0; printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n", wbuf); swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%La %LA %Le %LE %Lf %LF %Lg %LG", linfval, linfval, linfval, linfval, linfval, linfval, linfval, linfval); result |= wcscmp (wbuf, L"inf INF inf INF inf INF inf INF") != 0; printf ("expected L\"inf INF inf INF inf INF inf INF\", got L\"%S\"\n", wbuf); swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%La %LA %Le %LE %Lf %LF %Lg %LG", -linfval, -linfval, -linfval, -linfval, -linfval, -linfval, -linfval, -linfval); result |= wcscmp (wbuf, L"-inf -INF -inf -INF -inf -INF -inf -INF") != 0; printf ("expected L\"-inf -INF -inf -INF -inf -INF -inf -INF\", got L\"%S\"\n", wbuf); DIAG_POP_NEEDS_COMMENT; return result; }
float __ieee754_lgammaf_r(float x, int *signgamp) { float t,y,z,nadj,p,p1,p2,p3,q,r,w; int i,hx,ix; GET_FLOAT_WORD(hx,x); /* purge off +-inf, NaN, +-0, and negative arguments */ *signgamp = 1; ix = hx&0x7fffffff; if(__builtin_expect(ix>=0x7f800000, 0)) return x*x; if(__builtin_expect(ix==0, 0)) { if (hx < 0) *signgamp = -1; return one/fabsf(x); } if(__builtin_expect(ix<0x30800000, 0)) { /* |x|<2**-30, return -log(|x|) */ if(hx<0) { *signgamp = -1; return -__ieee754_logf(-x); } else return -__ieee754_logf(x); } if(hx<0) { if(ix>=0x4b000000) /* |x|>=2**23, must be -integer */ return x/zero; if (ix > 0x40000000 /* X < 2.0f. */ && ix < 0x41700000 /* X > -15.0f. */) return __lgamma_negf (x, signgamp); t = sin_pif(x); if(t==zero) return one/fabsf(t); /* -integer */ nadj = __ieee754_logf(pi/fabsf(t*x)); if(t<zero) *signgamp = -1; x = -x; } /* purge off 1 and 2 */ if (ix==0x3f800000||ix==0x40000000) r = 0; /* for x < 2.0 */ else if(ix<0x40000000) { if(ix<=0x3f666666) { /* lgamma(x) = lgamma(x+1)-log(x) */ r = -__ieee754_logf(x); if(ix>=0x3f3b4a20) {y = one-x; i= 0;} else if(ix>=0x3e6d3308) {y= x-(tc-one); i=1;} else {y = x; i=2;} } else { r = zero; if(ix>=0x3fdda618) {y=(float)2.0-x;i=0;} /* [1.7316,2] */ else if(ix>=0x3F9da620) {y=x-tc;i=1;} /* [1.23,1.73] */ else {y=x-one;i=2;} } switch(i) { case 0: z = y*y; p1 = a0+z*(a2+z*(a4+z*(a6+z*(a8+z*a10)))); p2 = z*(a1+z*(a3+z*(a5+z*(a7+z*(a9+z*a11))))); p = y*p1+p2; r += (p-(float)0.5*y); break; case 1: z = y*y; w = z*y; p1 = t0+w*(t3+w*(t6+w*(t9 +w*t12))); /* parallel comp */ p2 = t1+w*(t4+w*(t7+w*(t10+w*t13))); p3 = t2+w*(t5+w*(t8+w*(t11+w*t14))); p = z*p1-(tt-w*(p2+y*p3)); r += (tf + p); break; case 2: p1 = y*(u0+y*(u1+y*(u2+y*(u3+y*(u4+y*u5))))); p2 = one+y*(v1+y*(v2+y*(v3+y*(v4+y*v5)))); r += (-(float)0.5*y + p1/p2); } } else if(ix<0x41000000) { /* x < 8.0 */ i = (int)x; t = zero; y = x-(float)i; p = y*(s0+y*(s1+y*(s2+y*(s3+y*(s4+y*(s5+y*s6)))))); q = one+y*(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6))))); r = half*y+p/q; z = one; /* lgamma(1+s) = log(s) + lgamma(s) */ switch(i) { case 7: z *= (y+(float)6.0); /* FALLTHRU */ case 6: z *= (y+(float)5.0); /* FALLTHRU */ case 5: z *= (y+(float)4.0); /* FALLTHRU */ case 4: z *= (y+(float)3.0); /* FALLTHRU */ case 3: z *= (y+(float)2.0); /* FALLTHRU */ r += __ieee754_logf(z); break; } /* 8.0 <= x < 2**26 */ } else if (ix < 0x4c800000) { t = __ieee754_logf(x); z = one/x; y = z*z; w = w0+z*(w1+y*(w2+y*(w3+y*(w4+y*(w5+y*w6))))); r = (x-half)*(t-one)+w; } else /* 2**26 <= x <= inf */ r = math_narrow_eval (x*(__ieee754_logf(x)-one)); /* NADJ is set for negative arguments but not otherwise, resulting in warnings that it may be used uninitialized although in the cases where it is used it has always been set. */ DIAG_PUSH_NEEDS_COMMENT; #if __GNUC_PREREQ (4, 7) DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wmaybe-uninitialized"); #else DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wuninitialized"); #endif if(hx<0) r = nadj - r; DIAG_POP_NEEDS_COMMENT; return r; }
static int do_test (void) { char buf[25]; size_t i; int res = 0; int fd; mtrace (); strcpy (buf, "/tmp/test-vfprintfXXXXXX"); fd = mkstemp (buf); if (fd == -1) { printf ("cannot open temporary file: %m\n"); exit (1); } unlink (buf); for (i = 0; i < array_length (locs); ++i) { FILE *fp; struct stat st; int fd2; setlocale (LC_ALL, locs[i]); memset (large, '\1', sizeof (large)); large[sizeof (large) - 1] = '\0'; fd2 = dup (fd); if (fd2 == -1) { printf ("cannot dup for locale %s: %m\n", setlocale (LC_ALL, NULL)); exit (1); } if (ftruncate (fd2, 0) != 0) { printf ("cannot truncate file for locale %s: %m\n", setlocale (LC_ALL, NULL)); exit (1); } fp = fdopen (fd2, "a"); if (fp == NULL) { printf ("cannot create FILE for locale %s: %m\n", setlocale (LC_ALL, NULL)); exit (1); } fprintf (fp, "%s", large); fprintf (fp, "%.*s", 30000, large); large[20000] = '\0'; /* We're testing a large format string here and need to generate it to avoid this source file being ridiculous. So disable the warning about a generated format string. */ DIAG_PUSH_NEEDS_COMMENT; DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat-security"); fprintf (fp, large); DIAG_POP_NEEDS_COMMENT; fprintf (fp, "%-1.300000000s", "hello"); if (fflush (fp) != 0 || ferror (fp) != 0 || fclose (fp) != 0) { printf ("write error for locale %s: %m\n", setlocale (LC_ALL, NULL)); exit (1); } if (fstat (fd, &st) != 0) { printf ("cannot stat for locale %s: %m\n", setlocale (LC_ALL, NULL)); exit (1); } else if (st.st_size != 50000 + 30000 + 19999 + 5) { printf ("file size incorrect for locale %s: %jd instead of %jd\n", setlocale (LC_ALL, NULL), (intmax_t) st.st_size, (intmax_t) 50000 + 30000 + 19999 + 5); res = 1; } else printf ("locale %s OK\n", setlocale (LC_ALL, NULL)); } close (fd); return res; }
static int do_test (void) { const char blah[] = "BLAH"; char buf[strlen (blah) + 1]; FILE *fp, *f; const char *cp; char *wp; if ((fp = fdopen (fd, "w+")) == NULL) exit (1); flockfile (fp); f = fp; cp = blah; /* These tests deliberately use fwrite_unlocked with the size argument specified as 0, which results in "division by zero" warnings from the expansion of that macro (in code that is not evaluated for a size of 0). This applies to the tests of fread_unlocked below as well. */ DIAG_PUSH_NEEDS_COMMENT; DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdiv-by-zero"); if (ftello (fp) != 0 || fwrite_unlocked (blah, blah - blah, strlen (blah), f++) != 0 || f != fp + 1 || fwrite_unlocked ("", 5.0, 0, --f) != 0 || f != fp || fwrite_unlocked (cp++, 16, 0.25, fp) != 0 || cp != blah + 1 || fwrite_unlocked (--cp, 0.25, 16, fp) != 0 || cp != blah || fwrite_unlocked (blah, 0, -0.0, fp) != 0 || ftello (fp) != 0) { puts ("One of fwrite_unlocked tests failed"); exit (1); } DIAG_POP_NEEDS_COMMENT; if (fwrite_unlocked (blah, 1, strlen (blah) - 2, fp) != strlen (blah) - 2) { puts ("Could not write string into file"); exit (1); } if (putc_unlocked ('A' + 0x1000000, fp) != 'A') { puts ("putc_unlocked failed"); exit (1); } f = fp; cp = blah + strlen (blah) - 1; if (putc_unlocked (*cp++, f++) != 'H' || f != fp + 1 || cp != strchr (blah, '\0')) { puts ("fputc_unlocked failed"); exit (1); } if (ftello (fp) != (off_t) strlen (blah)) { printf ("Failed to write %zd bytes to temporary file", strlen (blah)); exit (1); } rewind (fp); f = fp; wp = buf; memset (buf, ' ', sizeof (buf)); /* See explanation above. */ DIAG_PUSH_NEEDS_COMMENT; DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdiv-by-zero"); if (ftello (fp) != 0 || fread_unlocked (buf, buf - buf, strlen (blah), f++) != 0 || f != fp + 1 || fread_unlocked (buf, 5.0, 0, --f) != 0 || f != fp || fread_unlocked (wp++, 16, 0.25, fp) != 0 || wp != buf + 1 || fread_unlocked (--wp, 0.25, 16, fp) != 0 || wp != buf || fread_unlocked (buf, 0, -0.0, fp) != 0 || ftello (fp) != 0 || memcmp (buf, " ", sizeof (buf)) != 0) { puts ("One of fread_unlocked tests failed"); exit (1); } DIAG_POP_NEEDS_COMMENT; if (fread_unlocked (buf, 1, strlen (blah) - 2, fp) != strlen (blah) - 2) { puts ("Could not read string from file"); exit (1); } if (getc_unlocked (fp) != 'A') { puts ("getc_unlocked failed"); exit (1); } f = fp; if (fgetc_unlocked (f++) != 'H' || f != fp + 1 || fgetc_unlocked (--f) != EOF || f != fp) { puts ("fgetc_unlocked failed"); exit (1); } if (ftello (fp) != (off_t) strlen (blah)) { printf ("Failed to read %zd bytes from temporary file", strlen (blah)); exit (1); } funlockfile (fp); fclose (fp); return 0; }
long double __kernel_tanl (long double x, long double y, int iy) { long double z, r, v, w, s; int32_t ix, sign; ieee854_long_double_shape_type u, u1; u.value = x; ix = u.parts32.w0 & 0x7fffffff; if (ix < 0x3fc60000) /* x < 2**-57 */ { if ((int) x == 0) { /* generate inexact */ if ((ix | u.parts32.w1 | u.parts32.w2 | u.parts32.w3 | (iy + 1)) == 0) return one / fabs (x); else return (iy == 1) ? x : -one / x; } } if (ix >= 0x3ffe5942) /* |x| >= 0.6743316650390625 */ { if ((u.parts32.w0 & 0x80000000) != 0) { x = -x; y = -y; sign = -1; } else sign = 1; z = pio4hi - x; w = pio4lo - y; x = z + w; y = 0.0; } z = x * x; r = T0 + z * (T1 + z * (T2 + z * (T3 + z * T4))); v = U0 + z * (U1 + z * (U2 + z * (U3 + z * (U4 + z)))); r = r / v; s = z * x; r = y + z * (s * r + y); r += TH * s; w = x + r; if (ix >= 0x3ffe5942) { v = (long double) iy; w = (v - 2.0 * (x - (w * w / (w + v) - r))); /* SIGN is set for arguments that reach this code, but not otherwise, resulting in warnings that it may be used uninitialized although in the cases where it is used it has always been set. */ DIAG_PUSH_NEEDS_COMMENT; #if __GNUC_PREREQ (4, 7) DIAG_IGNORE_NEEDS_COMMENT (5, "-Wmaybe-uninitialized"); #else DIAG_IGNORE_NEEDS_COMMENT (5, "-Wuninitialized"); #endif if (sign < 0) w = -w; DIAG_POP_NEEDS_COMMENT; return w; } if (iy == 1) return w; else { /* if allow error up to 2 ulp, simply return -1.0/(x+r) here */ /* compute -1.0/(x+r) accurately */ u1.value = w; u1.parts32.w2 = 0; u1.parts32.w3 = 0; v = r - (u1.value - x); /* u1+v = r+x */ z = -1.0 / w; u.value = z; u.parts32.w2 = 0; u.parts32.w3 = 0; s = 1.0 + u.value * u1.value; return u.value + z * (s + u.value * v); } }
long double __kernel_tanl (long double x, long double y, int iy) { long double z, r, v, w, s; int32_t ix, sign, hx, lx; double xhi; xhi = ldbl_high (x); EXTRACT_WORDS (hx, lx, xhi); ix = hx & 0x7fffffff; if (ix < 0x3c600000) /* x < 2**-57 */ { if ((int) x == 0) /* generate inexact */ { if ((ix | lx | (iy + 1)) == 0) return one / fabs (x); else if (iy == 1) { math_check_force_underflow (x); return x; } else return -one / x; } } if (ix >= 0x3fe59420) /* |x| >= 0.6743316650390625 */ { if ((hx & 0x80000000) != 0) { x = -x; y = -y; sign = -1; } else sign = 1; z = pio4hi - x; w = pio4lo - y; x = z + w; y = 0.0; } z = x * x; r = T0 + z * (T1 + z * (T2 + z * (T3 + z * T4))); v = U0 + z * (U1 + z * (U2 + z * (U3 + z * (U4 + z)))); r = r / v; s = z * x; r = y + z * (s * r + y); r += TH * s; w = x + r; if (ix >= 0x3fe59420) { v = (long double) iy; w = (v - 2.0 * (x - (w * w / (w + v) - r))); /* SIGN is set for arguments that reach this code, but not otherwise, resulting in warnings that it may be used uninitialized although in the cases where it is used it has always been set. */ DIAG_PUSH_NEEDS_COMMENT; DIAG_IGNORE_NEEDS_COMMENT (5, "-Wmaybe-uninitialized"); if (sign < 0) w = -w; DIAG_POP_NEEDS_COMMENT; return w; } if (iy == 1) return w; else { /* if allow error up to 2 ulp, simply return -1.0/(x+r) here */ /* compute -1.0/(x+r) accurately */ long double u1, z1; u1 = ldbl_high (w); v = r - (u1 - x); /* u1+v = r+x */ z = -1.0 / w; z1 = ldbl_high (z); s = 1.0 + z1 * u1; return z1 + z * (s + z1 * v); } }
static int do_test (void) { void *p; unsigned char *c; int save, i, ok; errno = 0; /* realloc (NULL, ...) behaves similarly to malloc (C89). */ DIAG_PUSH_NEEDS_COMMENT; #if __GNUC_PREREQ (7, 0) /* GCC 7 warns about too-large allocations; here we want to test that they fail. */ DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than="); #endif p = realloc (NULL, -1); DIAG_POP_NEEDS_COMMENT; save = errno; if (p != NULL) merror ("realloc (NULL, -1) succeeded."); /* errno should be set to ENOMEM on failure (POSIX). */ if (p == NULL && save != ENOMEM) merror ("errno is not set correctly"); errno = 0; /* realloc (NULL, ...) behaves similarly to malloc (C89). */ p = realloc (NULL, 10); save = errno; if (p == NULL) merror ("realloc (NULL, 10) failed."); free (p); p = calloc (20, 1); if (p == NULL) merror ("calloc (20, 1) failed."); /* Check increasing size preserves contents (C89). */ p = realloc (p, 200); if (p == NULL) merror ("realloc (p, 200) failed."); c = p; ok = 1; for (i = 0; i < 20; i++) { if (c[i] != 0) ok = 0; } if (ok == 0) merror ("first 20 bytes were not cleared"); free (p); p = realloc (NULL, 100); if (p == NULL) merror ("realloc (NULL, 100) failed."); memset (p, 0xff, 100); /* Check decreasing size preserves contents (C89). */ p = realloc (p, 16); if (p == NULL) merror ("realloc (p, 16) failed."); c = p; ok = 1; for (i = 0; i < 16; i++) { if (c[i] != 0xff) ok = 0; } if (ok == 0) merror ("first 16 bytes were not correct"); /* Check failed realloc leaves original untouched (C89). */ DIAG_PUSH_NEEDS_COMMENT; #if __GNUC_PREREQ (7, 0) /* GCC 7 warns about too-large allocations; here we want to test that they fail. */ DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than="); #endif c = realloc (p, -1); DIAG_POP_NEEDS_COMMENT; if (c != NULL) merror ("realloc (p, -1) succeeded."); c = p; ok = 1; for (i = 0; i < 16; i++) { if (c[i] != 0xff) ok = 0; } if (ok == 0) merror ("first 16 bytes were not correct after failed realloc"); /* realloc (p, 0) frees p (C89) and returns NULL (glibc). */ p = realloc (p, 0); if (p != NULL) merror ("realloc (p, 0) returned non-NULL."); /* realloc (NULL, 0) acts like malloc (0) (glibc). */ p = realloc (NULL, 0); if (p == NULL) merror ("realloc (NULL, 0) returned NULL."); free (p); return errors != 0; }
double __ieee754_lgamma_r(double x, int *signgamp) { double t,y,z,nadj,p,p1,p2,p3,q,r,w; int i,hx,lx,ix; EXTRACT_WORDS(hx,lx,x); /* purge off +-inf, NaN, +-0, and negative arguments */ *signgamp = 1; ix = hx&0x7fffffff; if(__builtin_expect(ix>=0x7ff00000, 0)) return x*x; if(__builtin_expect((ix|lx)==0, 0)) { if (hx < 0) *signgamp = -1; return one/fabs(x); } if(__builtin_expect(ix<0x3b900000, 0)) { /* |x|<2**-70, return -log(|x|) */ if(hx<0) { *signgamp = -1; return -__ieee754_log(-x); } else return -__ieee754_log(x); } if(hx<0) { if(__builtin_expect(ix>=0x43300000, 0)) /* |x|>=2**52, must be -integer */ return x/zero; t = sin_pi(x); if(t==zero) return one/fabsf(t); /* -integer */ nadj = __ieee754_log(pi/fabs(t*x)); if(t<zero) *signgamp = -1; x = -x; } /* purge off 1 and 2 */ if((((ix-0x3ff00000)|lx)==0)||(((ix-0x40000000)|lx)==0)) r = 0; /* for x < 2.0 */ else if(ix<0x40000000) { if(ix<=0x3feccccc) { /* lgamma(x) = lgamma(x+1)-log(x) */ r = -__ieee754_log(x); if(ix>=0x3FE76944) {y = one-x; i= 0;} else if(ix>=0x3FCDA661) {y= x-(tc-one); i=1;} else {y = x; i=2;} } else { r = zero; if(ix>=0x3FFBB4C3) {y=2.0-x;i=0;} /* [1.7316,2] */ else if(ix>=0x3FF3B4C4) {y=x-tc;i=1;} /* [1.23,1.73] */ else {y=x-one;i=2;} } switch(i) { case 0: z = y*y; p1 = a0+z*(a2+z*(a4+z*(a6+z*(a8+z*a10)))); p2 = z*(a1+z*(a3+z*(a5+z*(a7+z*(a9+z*a11))))); p = y*p1+p2; r += (p-0.5*y); break; case 1: z = y*y; w = z*y; p1 = t0+w*(t3+w*(t6+w*(t9 +w*t12))); /* parallel comp */ p2 = t1+w*(t4+w*(t7+w*(t10+w*t13))); p3 = t2+w*(t5+w*(t8+w*(t11+w*t14))); p = z*p1-(tt-w*(p2+y*p3)); r += (tf + p); break; case 2: p1 = y*(u0+y*(u1+y*(u2+y*(u3+y*(u4+y*u5))))); p2 = one+y*(v1+y*(v2+y*(v3+y*(v4+y*v5)))); r += (-0.5*y + p1/p2); } } else if(ix<0x40200000) { /* x < 8.0 */ i = (int)x; t = zero; y = x-(double)i; p = y*(s0+y*(s1+y*(s2+y*(s3+y*(s4+y*(s5+y*s6)))))); q = one+y*(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6))))); r = half*y+p/q; z = one; /* lgamma(1+s) = log(s) + lgamma(s) */ switch(i) { case 7: z *= (y+6.0); /* FALLTHRU */ case 6: z *= (y+5.0); /* FALLTHRU */ case 5: z *= (y+4.0); /* FALLTHRU */ case 4: z *= (y+3.0); /* FALLTHRU */ case 3: z *= (y+2.0); /* FALLTHRU */ r += __ieee754_log(z); break; } /* 8.0 <= x < 2**58 */ } else if (ix < 0x43900000) { t = __ieee754_log(x); z = one/x; y = z*z; w = w0+z*(w1+y*(w2+y*(w3+y*(w4+y*(w5+y*w6))))); r = (x-half)*(t-one)+w; } else /* 2**58 <= x <= inf */ r = x*(__ieee754_log(x)-one); /* NADJ is set for negative arguments but not otherwise, resulting in warnings that it may be used uninitialized although in the cases where it is used it has always been set. */ DIAG_PUSH_NEEDS_COMMENT; #if __GNUC_PREREQ (4, 7) DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wmaybe-uninitialized"); #else DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wuninitialized"); #endif if(hx<0) r = nadj - r; DIAG_POP_NEEDS_COMMENT; return r; }
static int do_test (void) { if (FE_ALL_EXCEPT == 0) { printf("Skipping test; no support for FP exceptions.\n"); return 0; } int except_mask = 0; #ifdef FE_DIVBYZERO except_mask |= FE_DIVBYZERO; #endif #ifdef FE_INVALID except_mask |= FE_INVALID; #endif #ifdef FE_OVERFLOW except_mask |= FE_OVERFLOW; #endif #ifdef FE_UNDERFLOW except_mask |= FE_UNDERFLOW; #endif int status = feenableexcept (except_mask); except_mask = fegetexcept (); if (except_mask == -1) { printf("\nBefore getcontext(): fegetexcept returned: %d\n", except_mask); return 1; } ucontext_t ctx; status = getcontext(&ctx); if (status) { printf("\ngetcontext failed, errno: %d.\n", errno); return 1; } printf ("\nDone with getcontext()!\n"); fflush (NULL); /* On nios2 and tilepro, GCC 5 warns that except_mask may be used uninitialized. Because it is always initialized and nothing in this test ever calls setcontext (a setcontext call could result in local variables being clobbered on the second return from getcontext), in fact an uninitialized use is not possible. */ DIAG_PUSH_NEEDS_COMMENT; DIAG_IGNORE_NEEDS_COMMENT (5, "-Wmaybe-uninitialized"); int mask = fegetexcept (); if (mask != except_mask) { printf("\nAfter getcontext(): fegetexcept returned: %d, expected: %d.\n", mask, except_mask); return 1; } printf("\nAt end fegetexcept() returned %d, expected: %d.\n", mask, except_mask); DIAG_POP_NEEDS_COMMENT; return 0; }
static int do_test (void) { int result = 0; pthread_attr_t a; cpu_set_t c1, c2; int err = pthread_attr_init (&a); if (err) { error (0, err, "pthread_attr_init failed"); result = 1; } err = pthread_attr_getaffinity_np (&a, sizeof (c1), &c1); if (err && err != ENOSYS) { error (0, err, "pthread_attr_getaffinity_np failed"); result = 1; } err = pthread_attr_destroy (&a); if (err) { error (0, err, "pthread_attr_destroy failed"); result = 1; } err = pthread_getattr_np (pthread_self (), &a); if (err) { error (0, err, "pthread_getattr_np failed"); result = 1; } int detachstate; err = pthread_attr_getdetachstate (&a, &detachstate); if (err) { error (0, err, "pthread_attr_getdetachstate failed"); result = 1; } else if (detachstate != PTHREAD_CREATE_JOINABLE) { error (0, 0, "initial thread not joinable"); result = 1; } void *stackaddr; size_t stacksize; err = pthread_attr_getstack (&a, &stackaddr, &stacksize); if (err) { error (0, err, "pthread_attr_getstack failed"); result = 1; } else if ((void *) &a < stackaddr || (void *) &a >= stackaddr + stacksize) { error (0, 0, "pthread_attr_getstack returned range does not cover main's stack"); result = 1; } else printf ("initial thread stack %p-%p (0x%zx)\n", stackaddr, stackaddr + stacksize, stacksize); size_t guardsize; err = pthread_attr_getguardsize (&a, &guardsize); if (err) { error (0, err, "pthread_attr_getguardsize failed"); result = 1; } else if (guardsize != 0) { error (0, 0, "pthread_attr_getguardsize returned %zd != 0", guardsize); result = 1; } int scope; err = pthread_attr_getscope (&a, &scope); if (err) { error (0, err, "pthread_attr_getscope failed"); result = 1; } else if (scope != PTHREAD_SCOPE_SYSTEM) { error (0, 0, "pthread_attr_getscope returned %d != PTHREAD_SCOPE_SYSTEM", scope); result = 1; } int inheritsched; err = pthread_attr_getinheritsched (&a, &inheritsched); if (err) { error (0, err, "pthread_attr_getinheritsched failed"); result = 1; } else if (inheritsched != PTHREAD_INHERIT_SCHED) { error (0, 0, "pthread_attr_getinheritsched returned %d != PTHREAD_INHERIT_SCHED", inheritsched); result = 1; } err = pthread_getaffinity_np (pthread_self (), sizeof (c1), &c1); if (err == 0) { err = pthread_attr_getaffinity_np (&a, sizeof (c2), &c2); if (err) { error (0, err, "pthread_attr_getaffinity_np failed"); result = 1; } else if (memcmp (&c1, &c2, sizeof (c1))) { error (0, 0, "pthread_attr_getaffinity_np returned different CPU mask than pthread_getattr_np"); result = 1; } } err = pthread_attr_destroy (&a); if (err) { error (0, err, "pthread_attr_destroy failed"); result = 1; } pthread_t th; err = pthread_create (&th, NULL, tf, NULL); if (err) { error (0, err, "pthread_create #1 failed"); result = 1; } else { void *ret; err = pthread_join (th, &ret); if (err) { error (0, err, "pthread_join #1 failed"); result = 1; } else if (ret != NULL) result = 1; } err = pthread_attr_init (&a); if (err) { error (0, err, "pthread_attr_init failed"); result = 1; } DIAG_PUSH_NEEDS_COMMENT; #if __GNUC_PREREQ (7, 0) /* GCC 8 warns about aliasing of the restrict-qualified arguments passed &a. Since pthread_create does not dereference its fourth argument, this aliasing, which is deliberate in this test, cannot in fact cause problems. */ DIAG_IGNORE_NEEDS_COMMENT (8, "-Wrestrict"); #endif err = pthread_create (&th, &a, tf, &a); DIAG_POP_NEEDS_COMMENT; if (err) { error (0, err, "pthread_create #2 failed"); result = 1; } else { void *ret; err = pthread_join (th, &ret); if (err) { error (0, err, "pthread_join #2 failed"); result = 1; } else if (ret != NULL) result = 1; } err = pthread_attr_setguardsize (&a, 16 * sysconf (_SC_PAGESIZE)); if (err) { error (0, err, "pthread_attr_setguardsize failed"); result = 1; } DIAG_PUSH_NEEDS_COMMENT; #if __GNUC_PREREQ (7, 0) /* GCC 8 warns about aliasing of the restrict-qualified arguments passed &a. Since pthread_create does not dereference its fourth argument, this aliasing, which is deliberate in this test, cannot in fact cause problems. */ DIAG_IGNORE_NEEDS_COMMENT (8, "-Wrestrict"); #endif err = pthread_create (&th, &a, tf, &a); DIAG_POP_NEEDS_COMMENT; if (err) { error (0, err, "pthread_create #3 failed"); result = 1; } else { void *ret; err = pthread_join (th, &ret); if (err) { error (0, err, "pthread_join #3 failed"); result = 1; } else if (ret != NULL) result = 1; } err = pthread_attr_destroy (&a); if (err) { error (0, err, "pthread_attr_destroy failed"); result = 1; } return result; }
long double __kernel_tanl (long double x, long double y, int iy) { long double z, r, v, w, s; long double absx = fabsl (x); int sign; if (absx < 0x1p-33) { if ((int) x == 0) { /* generate inexact */ if (x == 0 && iy == -1) return one / fabsl (x); else if (iy == 1) { math_check_force_underflow_nonneg (absx); return x; } else return -one / x; } } if (absx >= 0.6743316650390625L) { if (signbit (x)) { x = -x; y = -y; sign = -1; } else sign = 1; z = pio4hi - x; w = pio4lo - y; x = z + w; y = 0.0; } z = x * x; r = T0 + z * (T1 + z * (T2 + z * (T3 + z * T4))); v = U0 + z * (U1 + z * (U2 + z * (U3 + z * (U4 + z)))); r = r / v; s = z * x; r = y + z * (s * r + y); r += TH * s; w = x + r; if (absx >= 0.6743316650390625L) { v = (long double) iy; w = (v - 2.0 * (x - (w * w / (w + v) - r))); /* SIGN is set for arguments that reach this code, but not otherwise, resulting in warnings that it may be used uninitialized although in the cases where it is used it has always been set. */ DIAG_PUSH_NEEDS_COMMENT; DIAG_IGNORE_NEEDS_COMMENT (4.8, "-Wmaybe-uninitialized"); if (sign < 0) w = -w; DIAG_POP_NEEDS_COMMENT; return w; } if (iy == 1) return w; else return -1.0 / (x + r); }