Skip to content

Commit 741b017

Browse files
Merge pull request #2652 from fredrik-johansson/gr_write
Fix return values for `gr_write` methods
2 parents f058a95 + 3981633 commit 741b017

46 files changed

Lines changed: 460 additions & 456 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

doc/source/history.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ Bug fixes
2222
``"x1"``) on 32-bit glibc, which broke ``mpoly_test_irreducible`` and the
2323
``compose_mpoly`` tests on i386/armhf
2424
[EC, `#2648 <https://github.com/flintlib/flint/pull/2648>`_].
25+
* Fix return values for ``gr_write`` and ``gr_ctx_write methods``.
26+
Some ``gr_ctx_write`` implementations were incorrectly declared ``void``
27+
instead of ``int``, causing test failures on ppc64el. In addition,
28+
several methods ignored the return value from inner writes and would always
29+
return ``GR_SUCCESS`` even in the case of failure.
30+
The ``gr_stream_write`` family of functions have been marked
31+
``WARN_UNUSED_RESULT`` to catch similar bugs more easily.
32+
[FJ, `#2652 <https://github.com/flintlib/flint/pull/2652>`_].
33+
2534

2635
2026-04-24 -- FLINT 3.5.0
2736
-------------------------------------------------------------------------------

src/gr.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ void gr_stream_init_file(gr_stream_t out, FILE * fp);
8585
#endif
8686

8787
void gr_stream_init_str(gr_stream_t out);
88-
int gr_stream_write(gr_stream_t out, const char * s);
89-
int gr_stream_write_si(gr_stream_t out, slong x);
90-
int gr_stream_write_ui(gr_stream_t out, ulong x);
91-
int gr_stream_write_free(gr_stream_t out, char * s);
92-
int gr_stream_write_fmpz(gr_stream_t out, const fmpz_t x);
88+
WARN_UNUSED_RESULT int gr_stream_write(gr_stream_t out, const char * s);
89+
WARN_UNUSED_RESULT int gr_stream_write_si(gr_stream_t out, slong x);
90+
WARN_UNUSED_RESULT int gr_stream_write_ui(gr_stream_t out, ulong x);
91+
WARN_UNUSED_RESULT int gr_stream_write_free(gr_stream_t out, char * s);
92+
WARN_UNUSED_RESULT int gr_stream_write_fmpz(gr_stream_t out, const fmpz_t x);
9393

9494
#define GR_MUST_SUCCEED(expr) do { if ((expr) != GR_SUCCESS) { flint_throw(FLINT_ERROR, "GR_MUST_SUCCEED failure: %s", __FILE__); } } while (0)
9595
#define GR_IGNORE(expr) do { int ___unused = (expr); (void) ___unused; } while (0)

src/gr/acb.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ static int _gr_acb_ctx_get_real_prec(slong * res, gr_ctx_t ctx)
6161
static int
6262
_gr_acb_ctx_write(gr_stream_t out, gr_ctx_t ctx)
6363
{
64-
gr_stream_write(out, "Complex numbers (acb, prec = ");
65-
gr_stream_write_si(out, ACB_CTX_PREC(ctx));
66-
gr_stream_write(out, ")");
67-
return GR_SUCCESS;
64+
int status = GR_SUCCESS;
65+
status |= gr_stream_write(out, "Complex numbers (acb, prec = ");
66+
status |= gr_stream_write_si(out, ACB_CTX_PREC(ctx));
67+
status |= gr_stream_write(out, ")");
68+
return status;
6869
}
6970

7071
static void
@@ -105,60 +106,59 @@ _gr_acb_randtest(acb_t res, flint_rand_t state, const gr_ctx_t ctx)
105106
static int
106107
__gr_acb_write(gr_stream_t out, const acb_t x, slong digits, int flags, const gr_ctx_t ctx)
107108
{
109+
int status = GR_SUCCESS;
110+
108111
if (arb_is_zero(acb_imagref(x)))
109112
{
110113
/* used by polynomial printing */
111114
if (arb_is_exact(acb_realref(x)))
112115
{
113116
if (arf_is_zero(arb_midref(acb_realref(x))))
114117
{
115-
gr_stream_write(out, "0");
116-
return GR_SUCCESS;
118+
return gr_stream_write(out, "0");
117119
}
118120
else if (arf_is_one(arb_midref(acb_realref(x))))
119121
{
120-
gr_stream_write(out, "1");
121-
return GR_SUCCESS;
122+
return gr_stream_write(out, "1");
122123
}
123124
else if (arf_equal_si(arb_midref(acb_realref(x)), -1))
124125
{
125-
gr_stream_write(out, "-1");
126-
return GR_SUCCESS;
126+
return gr_stream_write(out, "-1");
127127
}
128128
}
129129

130-
gr_stream_write_free(out, arb_get_str(acb_realref(x), digits, flags));
130+
status |= gr_stream_write_free(out, arb_get_str(acb_realref(x), digits, flags));
131131
}
132132
else if (arb_is_zero(acb_realref(x)))
133133
{
134-
gr_stream_write_free(out, arb_get_str(acb_imagref(x), digits, flags));
135-
gr_stream_write(out, "*I");
134+
status |= gr_stream_write_free(out, arb_get_str(acb_imagref(x), digits, flags));
135+
status |= gr_stream_write(out, "*I");
136136
}
137137
else
138138
{
139-
gr_stream_write(out, "(");
140-
gr_stream_write_free(out, arb_get_str(acb_realref(x), digits, flags));
139+
status |= gr_stream_write(out, "(");
140+
status |= gr_stream_write_free(out, arb_get_str(acb_realref(x), digits, flags));
141141

142142
if ((arb_is_exact(acb_imagref(x)) || (flags & ARB_STR_NO_RADIUS))
143143
&& arf_sgn(arb_midref(acb_imagref(x))) < 0)
144144
{
145145
arb_t t;
146146
arb_init(t);
147147
arb_neg(t, acb_imagref(x));
148-
gr_stream_write(out, " - ");
149-
gr_stream_write_free(out, arb_get_str(t, digits, flags));
148+
status |= gr_stream_write(out, " - ");
149+
status |= gr_stream_write_free(out, arb_get_str(t, digits, flags));
150150
arb_clear(t);
151151
}
152152
else
153153
{
154-
gr_stream_write(out, " + ");
155-
gr_stream_write_free(out, arb_get_str(acb_imagref(x), digits, flags));
154+
status |= gr_stream_write(out, " + ");
155+
status |= gr_stream_write_free(out, arb_get_str(acb_imagref(x), digits, flags));
156156
}
157157

158-
gr_stream_write(out, "*I)");
158+
status |= gr_stream_write(out, "*I)");
159159
}
160160

161-
return GR_SUCCESS;
161+
return status;
162162
}
163163
static int
164164
_gr_acb_write(gr_stream_t out, const acb_t x, const gr_ctx_t ctx)

src/gr/acf.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ static int _gr_acf_ctx_get_real_prec(slong * res, gr_ctx_t ctx)
4949
static int
5050
_gr_acf_ctx_write(gr_stream_t out, gr_ctx_t ctx)
5151
{
52-
gr_stream_write(out, "Complex floating-point numbers (acf, prec = ");
53-
gr_stream_write_si(out, ACF_CTX_PREC(ctx));
54-
gr_stream_write(out, ")");
55-
return GR_SUCCESS;
52+
int status = GR_SUCCESS;
53+
status |= gr_stream_write(out, "Complex floating-point numbers (acf, prec = ");
54+
status |= gr_stream_write_si(out, ACF_CTX_PREC(ctx));
55+
status |= gr_stream_write(out, ")");
56+
return status;
5657
}
5758

5859
static void
@@ -96,38 +97,39 @@ static int
9697
_gr_acf_write(gr_stream_t out, const acf_t x, const gr_ctx_t ctx)
9798
{
9899
slong digits = ACF_CTX_PREC(ctx) * 0.30102999566398 + 1;
100+
int status = GR_SUCCESS;
99101

100102
if (arf_is_zero(acf_imagref(x)))
101103
{
102-
gr_stream_write_free(out, arf_get_str(acf_realref(x), digits));
104+
status |= gr_stream_write_free(out, arf_get_str(acf_realref(x), digits));
103105
}
104106
else if (arf_is_zero(acf_realref(x)))
105107
{
106-
gr_stream_write_free(out, arf_get_str(acf_imagref(x), digits));
107-
gr_stream_write(out, "*I");
108+
status |= gr_stream_write_free(out, arf_get_str(acf_imagref(x), digits));
109+
status |= gr_stream_write(out, "*I");
108110
}
109111
else
110112
{
111-
gr_stream_write(out, "(");
112-
gr_stream_write_free(out, arf_get_str(acf_realref(x), digits));
113+
status |= gr_stream_write(out, "(");
114+
status |= gr_stream_write_free(out, arf_get_str(acf_realref(x), digits));
113115

114116
if (arf_sgn(acf_imagref(x)) < 0)
115117
{
116118
arf_t t;
117119
arf_init_neg_shallow(t, acf_imagref(x));
118-
gr_stream_write(out, " - ");
119-
gr_stream_write_free(out, arf_get_str(t, digits));
120+
status |= gr_stream_write(out, " - ");
121+
status |= gr_stream_write_free(out, arf_get_str(t, digits));
120122
}
121123
else
122124
{
123-
gr_stream_write(out, " + ");
124-
gr_stream_write_free(out, arf_get_str(acf_imagref(x), digits));
125+
status |= gr_stream_write(out, " + ");
126+
status |= gr_stream_write_free(out, arf_get_str(acf_imagref(x), digits));
125127
}
126128

127-
gr_stream_write(out, "*I)");
129+
status |= gr_stream_write(out, "*I)");
128130
}
129131

130-
return GR_SUCCESS;
132+
return status;
131133
}
132134

133135
static int

src/gr/arb.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,11 @@ static int _gr_arb_ctx_get_real_prec(slong * res, gr_ctx_t ctx)
102102
static int
103103
_gr_arb_ctx_write(gr_stream_t out, gr_ctx_t ctx)
104104
{
105-
gr_stream_write(out, "Real numbers (arb, prec = ");
106-
gr_stream_write_si(out, ARB_CTX_PREC(ctx));
107-
gr_stream_write(out, ")");
108-
return GR_SUCCESS;
105+
int status = GR_SUCCESS;
106+
status |= gr_stream_write(out, "Real numbers (arb, prec = ");
107+
status |= gr_stream_write_si(out, ARB_CTX_PREC(ctx));
108+
status |= gr_stream_write(out, ")");
109+
return status;
109110
}
110111

111112
static void
@@ -151,32 +152,21 @@ _gr_arb_write(gr_stream_t out, const arb_t x, const gr_ctx_t ctx)
151152
if (arb_is_exact(x))
152153
{
153154
if (arf_is_zero(arb_midref(x)))
154-
{
155-
gr_stream_write(out, "0");
156-
return GR_SUCCESS;
157-
}
155+
return gr_stream_write(out, "0");
158156
else if (arf_is_one(arb_midref(x)))
159-
{
160-
gr_stream_write(out, "1");
161-
return GR_SUCCESS;
162-
}
157+
return gr_stream_write(out, "1");
163158
else if (arf_equal_si(arb_midref(x), -1))
164-
{
165-
gr_stream_write(out, "-1");
166-
return GR_SUCCESS;
167-
}
159+
return gr_stream_write(out, "-1");
168160
}
169161

170-
gr_stream_write_free(out, arb_get_str(x, ARB_CTX_PREC(ctx) * 0.30102999566398 + 1, 0));
171-
return GR_SUCCESS;
162+
return gr_stream_write_free(out, arb_get_str(x, ARB_CTX_PREC(ctx) * 0.30102999566398 + 1, 0));
172163
}
173164

174165
static int
175166
_gr_arb_write_n(gr_stream_t out, gr_srcptr x, slong n, gr_ctx_t ctx)
176167
{
177168
n = FLINT_MAX(n, 1);
178-
gr_stream_write_free(out, arb_get_str(x, n, ARB_STR_NO_RADIUS));
179-
return GR_SUCCESS;
169+
return gr_stream_write_free(out, arb_get_str(x, n, ARB_STR_NO_RADIUS));
180170
}
181171

182172
static int

src/gr/arf.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ static int _gr_arf_ctx_get_real_prec(slong * res, gr_ctx_t ctx)
4949
static int
5050
_gr_arf_ctx_write(gr_stream_t out, gr_ctx_t ctx)
5151
{
52-
gr_stream_write(out, "Floating-point numbers (arf, prec = ");
53-
gr_stream_write_si(out, ARF_CTX_PREC(ctx));
54-
gr_stream_write(out, ")");
55-
return GR_SUCCESS;
52+
int status = GR_SUCCESS;
53+
status |= gr_stream_write(out, "Floating-point numbers (arf, prec = ");
54+
status |= gr_stream_write_si(out, ARF_CTX_PREC(ctx));
55+
status |= gr_stream_write(out, ")");
56+
return status;
5657
}
5758

5859
static void
@@ -94,8 +95,7 @@ _gr_arf_randtest(arf_t res, flint_rand_t state, const gr_ctx_t ctx)
9495
static int
9596
_gr_arf_write(gr_stream_t out, const arf_t x, const gr_ctx_t ctx)
9697
{
97-
gr_stream_write_free(out, arf_get_str(x, ARF_CTX_PREC(ctx) * 0.30102999566398 + 1));
98-
return GR_SUCCESS;
98+
return gr_stream_write_free(out, arf_get_str(x, ARF_CTX_PREC(ctx) * 0.30102999566398 + 1));
9999
}
100100

101101
static int

src/gr/ca.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ static int
2828
_gr_ca_ctx_write(gr_stream_t out, gr_ctx_t ctx)
2929
{
3030
if (ctx->which_ring == GR_CTX_RR_CA)
31-
gr_stream_write(out, "Real numbers (ca)");
31+
return gr_stream_write(out, "Real numbers (ca)");
3232
else if (ctx->which_ring == GR_CTX_CC_CA)
33-
gr_stream_write(out, "Complex numbers (ca)");
33+
return gr_stream_write(out, "Complex numbers (ca)");
3434
else if (ctx->which_ring == GR_CTX_REAL_ALGEBRAIC_CA)
35-
gr_stream_write(out, "Real algebraic numbers (ca)");
35+
return gr_stream_write(out, "Real algebraic numbers (ca)");
3636
else if (ctx->which_ring == GR_CTX_COMPLEX_ALGEBRAIC_CA)
37-
gr_stream_write(out, "Complex algebraic numbers (ca)");
37+
return gr_stream_write(out, "Complex algebraic numbers (ca)");
3838
else if (ctx->which_ring == GR_CTX_COMPLEX_EXTENDED_CA)
39-
gr_stream_write(out, "Complex numbers + extended values (ca)");
40-
return GR_SUCCESS;
39+
return gr_stream_write(out, "Complex numbers + extended values (ca)");
40+
return GR_UNABLE;
4141
}
4242

4343
void
@@ -123,8 +123,7 @@ _gr_ca_randtest(ca_t res, flint_rand_t state, gr_ctx_t ctx)
123123
static int
124124
_gr_ca_write(gr_stream_t out, const ca_t x, gr_ctx_t ctx)
125125
{
126-
gr_stream_write_free(out, ca_get_str(x, GR_CA_CTX(ctx)));
127-
return GR_SUCCESS;
126+
return gr_stream_write_free(out, ca_get_str(x, GR_CA_CTX(ctx)));
128127
}
129128

130129
static int

src/gr/complex.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ typedef gr_complex_ctx_struct gr_complex_ctx_t[1];
3838
static int
3939
_gr_complex_ctx_write(gr_stream_t out, gr_complex_ctx_t ctx)
4040
{
41-
gr_stream_write(out, "Complex algebra over ");
42-
gr_ctx_write(out, REAL_CTX(ctx));
43-
return GR_SUCCESS;
41+
int status = GR_SUCCESS;
42+
status |= gr_stream_write(out, "Complex algebra over ");
43+
status |= gr_ctx_write(out, REAL_CTX(ctx));
44+
return status;
4445
}
4546

4647
static void

src/gr/dirichlet.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717

1818
static int _gr_dirichlet_ctx_write(gr_stream_t out, gr_ctx_t ctx)
1919
{
20-
gr_stream_write(out, "Dirichlet group modulo ");
21-
gr_stream_write_ui(out, DIRICHLET_CTX(ctx)->q);
22-
gr_stream_write(out, " (dirichlet_char)");
23-
return GR_SUCCESS;
20+
int status = GR_SUCCESS;
21+
status |= gr_stream_write(out, "Dirichlet group modulo ");
22+
status |= gr_stream_write_ui(out, DIRICHLET_CTX(ctx)->q);
23+
status |= gr_stream_write(out, " (dirichlet_char)");
24+
return status;
2425
}
2526

2627
static void
@@ -55,34 +56,35 @@ _gr_dirichlet_swap(dirichlet_char_t x, dirichlet_char_t y, gr_ctx_t ctx)
5556
*y = t;
5657
}
5758

58-
static void
59+
static int
5960
_dirichlet_char_print(gr_stream_t out, const dirichlet_group_t G, const dirichlet_char_t x)
6061
{
61-
gr_stream_write(out, "chi_");
62-
gr_stream_write_ui(out, G->q);
63-
gr_stream_write(out, "(");
64-
gr_stream_write_ui(out, G->q == 1 ? 1 : x->n);
65-
gr_stream_write(out, ", .)");
62+
int status = GR_SUCCESS;
63+
status |= gr_stream_write(out, "chi_");
64+
status |= gr_stream_write_ui(out, G->q);
65+
status |= gr_stream_write(out, "(");
66+
status |= gr_stream_write_ui(out, G->q == 1 ? 1 : x->n);
67+
status |= gr_stream_write(out, ", .)");
6668
/*
6769
slong k;
68-
gr_stream_write(out, "Character ");
69-
gr_stream_write_ui(out, G->q == 1 ? 1 : x->n);
70-
gr_stream_write(out, " [");
70+
status |= gr_stream_write(out, "Character ");
71+
status |= gr_stream_write_ui(out, G->q == 1 ? 1 : x->n);
72+
status |= gr_stream_write(out, " [");
7173
for (k = 0; k < G->num; k++)
7274
{
73-
gr_stream_write_ui(out, x->log[k]);
75+
status |= gr_stream_write_ui(out, x->log[k]);
7476
if (k + 1 < G->num)
75-
gr_stream_write(out, ", ");
77+
status |= gr_stream_write(out, ", ");
7678
}
77-
gr_stream_write(out, "]");
79+
status |= gr_stream_write(out, "]");
7880
*/
81+
return status;
7982
}
8083

8184
static int
8285
_gr_dirichlet_write(gr_stream_t out, dirichlet_char_t x, gr_ctx_t ctx)
8386
{
84-
_dirichlet_char_print(out, DIRICHLET_CTX(ctx), x);
85-
return GR_SUCCESS;
87+
return _dirichlet_char_print(out, DIRICHLET_CTX(ctx), x);
8688
}
8789

8890
static int

0 commit comments

Comments
 (0)