home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / gnu / g / lib / bug / 795 < prev    next >
Encoding:
Text File  |  1993-01-21  |  5.3 KB  |  201 lines

  1. Path: sparky!uunet!paladin.american.edu!howland.reston.ans.net!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!cis.ohio-state.edu!cygnus.com!bothner
  2. From: bothner@cygnus.com
  3. Newsgroups: gnu.g++.lib.bug
  4. Subject: Re: 2.3: width(x) off by 1 for istreams
  5. Date: 21 Jan 1993 18:22:51 -0500
  6. Organization: GNUs Not Usenet
  7. Lines: 187
  8. Sender: daemon@cis.ohio-state.edu
  9. Approved: bug-lib-g++@prep.ai.mit.edu
  10. Distribution: gnu
  11. Message-ID: <9301212244.AA18937@cygnus.com>
  12. References: <9301201947.AA01121@cs.rice.edu>
  13.  
  14. > The width attribute of an istream is supposed to limit the number of
  15. > characters read into a char* variable to prevent overflowing a buffer.
  16.  
  17. The following patch should fix this.  It also should suppress
  18. print of '+' for unsigned values.
  19.  
  20.     --Per Bothner
  21. Cygnus Support     bothner@cygnus.com
  22.  
  23. ===================================================================
  24. RCS file: /rel/cvsfiles/devo/libg++/iostream/iostream.C,v
  25. retrieving revision 1.34
  26. diff -c -r1.34 iostream.C
  27. *** 1.34    1993/01/17 02:08:11
  28. --- iostream.C    1993/01/21 22:33:33
  29. ***************
  30. *** 157,163 ****
  31.           set(ios::eofbit|ios::failbit);
  32.       else {
  33.           int w = width(0);
  34. !         *ptr++ = ch;
  35.           for (;;) {
  36.           ch = sb->sbumpc();
  37.           if (ch == EOF) {
  38. --- 157,163 ----
  39.           set(ios::eofbit|ios::failbit);
  40.       else {
  41.           int w = width(0);
  42. !         sb->sputbackc(ch);
  43.           for (;;) {
  44.           ch = sb->sbumpc();
  45.           if (ch == EOF) {
  46. ***************
  47. *** 342,348 ****
  48.       return *this;
  49.   }
  50.   
  51. ! static void write_int(ostream& stream, unsigned LONGEST val, int neg)
  52.   {
  53.   #define WRITE_BUF_SIZE (10 + sizeof(unsigned LONGEST) * 3)
  54.       char buf[WRITE_BUF_SIZE];
  55. --- 342,353 ----
  56.       return *this;
  57.   }
  58.   
  59. ! /* Write VAL on STREAM.
  60. !    If SIGN<0, val is the absolute value of a negative number.
  61. !    If SIGN>0, val is a signed non-negative number.
  62. !    If SIGN==0, val is unsigned. */
  63. ! static void write_int(ostream& stream, unsigned LONGEST val, int sign)
  64.   {
  65.   #define WRITE_BUF_SIZE (10 + sizeof(unsigned LONGEST) * 3)
  66.       char buf[WRITE_BUF_SIZE];
  67. ***************
  68. *** 349,354 ****
  69. --- 354,360 ----
  70.       register char *buf_ptr = buf+WRITE_BUF_SIZE; // End of buf.
  71.       char *show_base = "";
  72.       int show_base_len = 0;
  73. +     int show_pos = 0; // If 1, print a '+'.
  74.   
  75.       // Now do the actual conversion, placing the result at the *end* of buf.
  76.       // Note that we use separate code for decimal, octal, and hex,
  77. ***************
  78. *** 374,379 ****
  79. --- 380,387 ----
  80.       }
  81.       }
  82.       else { // Decimal
  83. +     if (val != 0 && sign > 0 && (stream.flags() & ios::showpos))
  84. +         show_pos=1;
  85.   #ifdef __GNUC__
  86.       // Optimization:  Only use long long when we need to.
  87.       while (val > UINT_MAX) {
  88. ***************
  89. *** 393,404 ****
  90.   
  91.       int buf_len = buf+WRITE_BUF_SIZE - buf_ptr;
  92.       int w = stream.width(0);
  93. -     int show_pos = 0;
  94.   
  95.       // Calculate padding.
  96. !     int len = buf_len;
  97. !     if (neg) len++;
  98. !     else if (val != 0 && (stream.flags() & ios::showpos)) len++, show_pos=1;
  99.       len += show_base_len;
  100.       int padding = len > w ? 0 : w - len;
  101.   
  102. --- 401,410 ----
  103.   
  104.       int buf_len = buf+WRITE_BUF_SIZE - buf_ptr;
  105.       int w = stream.width(0);
  106.   
  107.       // Calculate padding.
  108. !     int len = buf_len+show_pos;
  109. !     if (sign < 0) len++;
  110.       len += show_base_len;
  111.       int padding = len > w ? 0 : w - len;
  112.   
  113. ***************
  114. *** 411,417 ****
  115.       && pad_kind != (ios::fmtflags)ios::left
  116.       && pad_kind != (ios::fmtflags)ios::internal) // Default (right) adjust.
  117.       sbuf->padn(fill_char, padding);
  118. !     if (neg) sbuf->sputc('-');
  119.       else if (show_pos) sbuf->sputc('+');
  120.       if (show_base_len)
  121.       sbuf->sputn(show_base, show_base_len);
  122. --- 417,423 ----
  123.       && pad_kind != (ios::fmtflags)ios::left
  124.       && pad_kind != (ios::fmtflags)ios::internal) // Default (right) adjust.
  125.       sbuf->padn(fill_char, padding);
  126. !     if (sign < 0) sbuf->sputc('-');
  127.       else if (show_pos) sbuf->sputc('+');
  128.       if (show_base_len)
  129.       sbuf->sputn(show_base, show_base_len);
  130. ***************
  131. *** 426,435 ****
  132.   ostream& ostream::operator<<(int n)
  133.   {
  134.       if (opfx()) {
  135. !     int neg = 0;
  136.       if (n < 0 && (flags() & (ios::oct|ios::hex)) == 0)
  137. !         n = -n, neg = 1;
  138. !     write_int(*this, n, neg);
  139.       }
  140.       return *this;
  141.   }
  142. --- 432,441 ----
  143.   ostream& ostream::operator<<(int n)
  144.   {
  145.       if (opfx()) {
  146. !     int sign = 1;
  147.       if (n < 0 && (flags() & (ios::oct|ios::hex)) == 0)
  148. !         n = -n, sign = -1;
  149. !     write_int(*this, n, sign);
  150.       }
  151.       return *this;
  152.   }
  153. ***************
  154. *** 445,454 ****
  155.   ostream& ostream::operator<<(long n)
  156.   {
  157.       if (opfx()) {
  158. !     int neg = 0;
  159.       if (n < 0 && (flags() & (ios::oct|ios::hex)) == 0)
  160. !         n = -n, neg = 1;
  161. !     write_int(*this, n, neg);
  162.       }
  163.       return *this;
  164.   }
  165. --- 451,460 ----
  166.   ostream& ostream::operator<<(long n)
  167.   {
  168.       if (opfx()) {
  169. !     int sign = 1;
  170.       if (n < 0 && (flags() & (ios::oct|ios::hex)) == 0)
  171. !         n = -n, sign = -1;
  172. !     write_int(*this, n, sign);
  173.       }
  174.       return *this;
  175.   }
  176. ***************
  177. *** 464,473 ****
  178.   ostream& ostream::operator<<(long long n)
  179.   {
  180.       if (opfx()) {
  181. !     int neg = 0;
  182.       if (n < 0 && (flags() & (ios::oct|ios::hex)) == 0)
  183. !         n = -n, neg = 1;
  184. !     write_int(*this, n, neg);
  185.       }
  186.       return *this;
  187.   }
  188. --- 470,479 ----
  189.   ostream& ostream::operator<<(long long n)
  190.   {
  191.       if (opfx()) {
  192. !     int sign = 1;
  193.       if (n < 0 && (flags() & (ios::oct|ios::hex)) == 0)
  194. !         n = -n, sign = -1;
  195. !     write_int(*this, n, sign);
  196.       }
  197.       return *this;
  198.   }
  199.  
  200.