Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e143598

Browse files
committed
Fix memory stomp that's turning the whole buildfarm pink: you can't hack up
pg_wcsformat without changing pg_wcssize to match. Add some comments to try to make that clearer, and make a couple other minor editorializations.
1 parent c56b444 commit e143598

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

src/bin/psql/mbprint.c

+26-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2008, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/mbprint.c,v 1.32 2008/05/08 19:11:36 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/mbprint.c,v 1.33 2008/05/09 05:25:04 tgl Exp $
77
*
88
* XXX this file does not really belong in psql/. Perhaps move to libpq?
99
* It also seems that the mbvalidate function is redundant with existing
@@ -205,12 +205,15 @@ pg_wcswidth(const unsigned char *pwcs, size_t len, int encoding)
205205
* pg_wcssize takes the given string in the given encoding and returns three
206206
* values:
207207
* result_width: Width in display characters of the longest line in string
208-
* result_height: Number of newlines in display output
209-
* result_format_size: Number of bytes required to store formatted representation of string
208+
* result_height: Number of lines in display output
209+
* result_format_size: Number of bytes required to store formatted
210+
* representation of string
211+
*
212+
* This MUST be kept in sync with pg_wcsformat!
210213
*/
211-
int
212-
pg_wcssize(unsigned char *pwcs, size_t len, int encoding, int *result_width,
213-
int *result_height, int *result_format_size)
214+
void
215+
pg_wcssize(unsigned char *pwcs, size_t len, int encoding,
216+
int *result_width, int *result_height, int *result_format_size)
214217
{
215218
int w,
216219
chlen = 0,
@@ -241,6 +244,14 @@ pg_wcssize(unsigned char *pwcs, size_t len, int encoding, int *result_width,
241244
linewidth += 2;
242245
format_size += 2;
243246
}
247+
else if (*pwcs == '\t') /* Tab */
248+
{
249+
do
250+
{
251+
linewidth++;
252+
format_size++;
253+
} while (linewidth % 8 != 0);
254+
}
244255
else if (w < 0) /* Other control char */
245256
{
246257
linewidth += 4;
@@ -266,7 +277,7 @@ pg_wcssize(unsigned char *pwcs, size_t len, int encoding, int *result_width,
266277
}
267278
if (linewidth > width)
268279
width = linewidth;
269-
format_size += 1;
280+
format_size += 1; /* For NUL char */
270281

271282
/* Set results */
272283
if (result_width)
@@ -275,14 +286,13 @@ pg_wcssize(unsigned char *pwcs, size_t len, int encoding, int *result_width,
275286
*result_height = height;
276287
if (result_format_size)
277288
*result_format_size = format_size;
278-
279-
return width;
280289
}
281290

282291
/*
283-
* Filter out unprintable characters, companion to wcs_size.
284-
* Break input into lines based on \n. lineptr[i].ptr == NULL
285-
* indicates the end of the array.
292+
* Format a string into one or more "struct lineptr" lines.
293+
* lines[i].ptr == NULL indicates the end of the array.
294+
*
295+
* This MUST be kept in sync with pg_wcssize!
286296
*/
287297
void
288298
pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
@@ -309,7 +319,7 @@ pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
309319
linewidth = 0;
310320
lines++;
311321
count--;
312-
if (count == 0)
322+
if (count <= 0)
313323
exit(1); /* Screwup */
314324

315325
/* make next line point to remaining memory */
@@ -346,14 +356,14 @@ pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
346356
if (encoding == PG_UTF8)
347357
sprintf((char *) ptr, "\\u%04X", utf2ucs(pwcs));
348358
else
349-
359+
{
350360
/*
351361
* This case cannot happen in the current code because only
352362
* UTF-8 signals multibyte control characters. But we may need
353363
* to support it at some stage
354364
*/
355365
sprintf((char *) ptr, "\\u????");
356-
366+
}
357367
ptr += 6;
358368
linewidth += 6;
359369
}
@@ -370,7 +380,7 @@ pg_wcsformat(unsigned char *pwcs, size_t len, int encoding,
370380
lines->width = linewidth;
371381
*ptr++ = '\0'; /* Terminate formatted string */
372382

373-
if (count == 0)
383+
if (count <= 0)
374384
exit(1); /* Screwup */
375385

376386
(lines+1)->ptr = NULL; /* terminate line array */

src/bin/psql/mbprint.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/bin/psql/mbprint.h,v 1.11 2006/10/04 00:30:06 momjian Exp $ */
1+
/* $PostgreSQL: pgsql/src/bin/psql/mbprint.h,v 1.12 2008/05/09 05:25:04 tgl Exp $ */
22
#ifndef MBPRINT_H
33
#define MBPRINT_H
44

@@ -13,6 +13,7 @@ extern unsigned char *mbvalidate(unsigned char *pwcs, int encoding);
1313

1414
extern int pg_wcswidth(const unsigned char *pwcs, size_t len, int encoding);
1515
extern void pg_wcsformat(unsigned char *pwcs, size_t len, int encoding, struct lineptr * lines, int count);
16-
extern int pg_wcssize(unsigned char *pwcs, size_t len, int encoding, int *width, int *height, int *format_size);
16+
extern void pg_wcssize(unsigned char *pwcs, size_t len, int encoding,
17+
int *width, int *height, int *format_size);
1718

1819
#endif /* MBPRINT_H */

0 commit comments

Comments
 (0)