|
120 | 120 |
|
121 | 121 |
|
122 | 122 | /* sort-type codes for sort__start probes */ |
123 | | -#define HEAP_SORT 0 |
124 | | -#define INDEX_SORT 1 |
125 | | -#define DATUM_SORT 2 |
| 123 | +#define HEAP_SORT 0 |
| 124 | +#define INDEX_SORT 1 |
| 125 | +#define DATUM_SORT 2 |
126 | 126 | #define CLUSTER_SORT 3 |
127 | 127 |
|
128 | 128 | /* GUC variables */ |
@@ -435,6 +435,13 @@ struct Tuplesortstate |
435 | 435 | * a lot better than what we were doing before 7.3. |
436 | 436 | */ |
437 | 437 |
|
| 438 | +/* When using this macro, beware of double evaluation of len */ |
| 439 | +#define LogicalTapeReadExact(tapeset, tapenum, ptr, len) \ |
| 440 | + do { \ |
| 441 | + if (LogicalTapeRead(tapeset, tapenum, ptr, len) != (size_t) (len)) \ |
| 442 | + elog(ERROR, "unexpected end of data"); \ |
| 443 | + } while(0) |
| 444 | + |
438 | 445 |
|
439 | 446 | static Tuplesortstate *tuplesort_begin_common(int workMem, bool randomAccess); |
440 | 447 | static void puttuple_common(Tuplesortstate *state, SortTuple *tuple); |
@@ -2576,8 +2583,8 @@ getlen(Tuplesortstate *state, int tapenum, bool eofOK) |
2576 | 2583 | { |
2577 | 2584 | unsigned int len; |
2578 | 2585 |
|
2579 | | - if (LogicalTapeRead(state->tapeset, tapenum, (void *) &len, |
2580 | | - sizeof(len)) != sizeof(len)) |
| 2586 | + if (LogicalTapeRead(state->tapeset, tapenum, |
| 2587 | + &len, sizeof(len)) != sizeof(len)) |
2581 | 2588 | elog(ERROR, "unexpected end of tape"); |
2582 | 2589 | if (len == 0 && !eofOK) |
2583 | 2590 | elog(ERROR, "unexpected end of data"); |
@@ -2810,14 +2817,11 @@ readtup_heap(Tuplesortstate *state, SortTuple *stup, |
2810 | 2817 | USEMEM(state, GetMemoryChunkSpace(tuple)); |
2811 | 2818 | /* read in the tuple proper */ |
2812 | 2819 | tuple->t_len = tuplen; |
2813 | | - if (LogicalTapeRead(state->tapeset, tapenum, |
2814 | | - (void *) tupbody, |
2815 | | - tupbodylen) != (size_t) tupbodylen) |
2816 | | - elog(ERROR, "unexpected end of data"); |
| 2820 | + LogicalTapeReadExact(state->tapeset, tapenum, |
| 2821 | + tupbody, tupbodylen); |
2817 | 2822 | if (state->randomAccess) /* need trailing length word? */ |
2818 | | - if (LogicalTapeRead(state->tapeset, tapenum, (void *) &tuplen, |
2819 | | - sizeof(tuplen)) != sizeof(tuplen)) |
2820 | | - elog(ERROR, "unexpected end of data"); |
| 2823 | + LogicalTapeReadExact(state->tapeset, tapenum, |
| 2824 | + &tuplen, sizeof(tuplen)); |
2821 | 2825 | stup->tuple = (void *) tuple; |
2822 | 2826 | /* set up first-column key value */ |
2823 | 2827 | htup.t_len = tuple->t_len + MINIMAL_TUPLE_OFFSET; |
@@ -2998,20 +3002,16 @@ readtup_cluster(Tuplesortstate *state, SortTuple *stup, |
2998 | 3002 | /* Reconstruct the HeapTupleData header */ |
2999 | 3003 | tuple->t_data = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE); |
3000 | 3004 | tuple->t_len = t_len; |
3001 | | - if (LogicalTapeRead(state->tapeset, tapenum, |
3002 | | - &tuple->t_self, |
3003 | | - sizeof(ItemPointerData)) != sizeof(ItemPointerData)) |
3004 | | - elog(ERROR, "unexpected end of data"); |
| 3005 | + LogicalTapeReadExact(state->tapeset, tapenum, |
| 3006 | + &tuple->t_self, sizeof(ItemPointerData)); |
3005 | 3007 | /* We don't currently bother to reconstruct t_tableOid */ |
3006 | 3008 | tuple->t_tableOid = InvalidOid; |
3007 | 3009 | /* Read in the tuple body */ |
3008 | | - if (LogicalTapeRead(state->tapeset, tapenum, |
3009 | | - tuple->t_data, tuple->t_len) != tuple->t_len) |
3010 | | - elog(ERROR, "unexpected end of data"); |
| 3010 | + LogicalTapeReadExact(state->tapeset, tapenum, |
| 3011 | + tuple->t_data, tuple->t_len); |
3011 | 3012 | if (state->randomAccess) /* need trailing length word? */ |
3012 | | - if (LogicalTapeRead(state->tapeset, tapenum, &tuplen, |
3013 | | - sizeof(tuplen)) != sizeof(tuplen)) |
3014 | | - elog(ERROR, "unexpected end of data"); |
| 3013 | + LogicalTapeReadExact(state->tapeset, tapenum, |
| 3014 | + &tuplen, sizeof(tuplen)); |
3015 | 3015 | stup->tuple = (void *) tuple; |
3016 | 3016 | /* set up first-column key value, if it's a simple column */ |
3017 | 3017 | if (state->indexInfo->ii_KeyAttrNumbers[0] != 0) |
@@ -3243,13 +3243,11 @@ readtup_index(Tuplesortstate *state, SortTuple *stup, |
3243 | 3243 | IndexTuple tuple = (IndexTuple) palloc(tuplen); |
3244 | 3244 |
|
3245 | 3245 | USEMEM(state, GetMemoryChunkSpace(tuple)); |
3246 | | - if (LogicalTapeRead(state->tapeset, tapenum, (void *) tuple, |
3247 | | - tuplen) != tuplen) |
3248 | | - elog(ERROR, "unexpected end of data"); |
| 3246 | + LogicalTapeReadExact(state->tapeset, tapenum, |
| 3247 | + tuple, tuplen); |
3249 | 3248 | if (state->randomAccess) /* need trailing length word? */ |
3250 | | - if (LogicalTapeRead(state->tapeset, tapenum, (void *) &tuplen, |
3251 | | - sizeof(tuplen)) != sizeof(tuplen)) |
3252 | | - elog(ERROR, "unexpected end of data"); |
| 3249 | + LogicalTapeReadExact(state->tapeset, tapenum, |
| 3250 | + &tuplen, sizeof(tuplen)); |
3253 | 3251 | stup->tuple = (void *) tuple; |
3254 | 3252 | /* set up first-column key value */ |
3255 | 3253 | stup->datum1 = index_getattr(tuple, |
@@ -3357,29 +3355,26 @@ readtup_datum(Tuplesortstate *state, SortTuple *stup, |
3357 | 3355 | else if (state->datumTypeByVal) |
3358 | 3356 | { |
3359 | 3357 | Assert(tuplen == sizeof(Datum)); |
3360 | | - if (LogicalTapeRead(state->tapeset, tapenum, (void *) &stup->datum1, |
3361 | | - tuplen) != tuplen) |
3362 | | - elog(ERROR, "unexpected end of data"); |
| 3358 | + LogicalTapeReadExact(state->tapeset, tapenum, |
| 3359 | + &stup->datum1, tuplen); |
3363 | 3360 | stup->isnull1 = false; |
3364 | 3361 | stup->tuple = NULL; |
3365 | 3362 | } |
3366 | 3363 | else |
3367 | 3364 | { |
3368 | 3365 | void *raddr = palloc(tuplen); |
3369 | 3366 |
|
3370 | | - if (LogicalTapeRead(state->tapeset, tapenum, raddr, |
3371 | | - tuplen) != tuplen) |
3372 | | - elog(ERROR, "unexpected end of data"); |
| 3367 | + LogicalTapeReadExact(state->tapeset, tapenum, |
| 3368 | + raddr, tuplen); |
3373 | 3369 | stup->datum1 = PointerGetDatum(raddr); |
3374 | 3370 | stup->isnull1 = false; |
3375 | 3371 | stup->tuple = raddr; |
3376 | 3372 | USEMEM(state, GetMemoryChunkSpace(raddr)); |
3377 | 3373 | } |
3378 | 3374 |
|
3379 | 3375 | if (state->randomAccess) /* need trailing length word? */ |
3380 | | - if (LogicalTapeRead(state->tapeset, tapenum, (void *) &tuplen, |
3381 | | - sizeof(tuplen)) != sizeof(tuplen)) |
3382 | | - elog(ERROR, "unexpected end of data"); |
| 3376 | + LogicalTapeReadExact(state->tapeset, tapenum, |
| 3377 | + &tuplen, sizeof(tuplen)); |
3383 | 3378 | } |
3384 | 3379 |
|
3385 | 3380 | static void |
|
0 commit comments