From: daz Date: 2009-02-24T12:31:39+09:00 Subject: [ruby-core:22393] Re: [Feature #908] Should be an easy way of reading N characters from am I/O stream Michael Selig wrote: > > However I still do not understand why reading N characters (instead of > the current "limit" implementation) might be described as being > "insecure". Can someone please explain it? It is clear that the string > returned may be larger than N bytes long, but why is that "insecure"? > > Cheers > Mike. > > A definition of "character" is insecure and the IO class isn't the correct place to lock a defintion into the language. With an example from the future I can defeat your understanding of the word: In WallyCode - don't Google it - character codes are assigned to common words, such as ... 0x112233 = "Hello" 0x445566 = "World" ... where a complete character code is 3-bytes long. If you provide a buffer that's at least 6-bytes long, IO can return two valid codes (the things you refer to as "characters"). However, the resulting WallyString would contain 10 characters ("HelloWorld") and exceed the length of the buffer provided. Of course, the WallyString class will truncate the result to "HelloW" but the IO pointer is one character code (3-bytes) too far advanced. Implementing "Read(N)" this at the wrong level could result in compromising the extensibilty of the encoding system and create problems, later on, like when those folks decided that 2-bytes was enough to store the year or that a signed 32-bit offset from 1970 was adequate for a datestamp. As you only have to consider your local application (whereas Ruby has to consider _all_ possible uses), an implementation is more likely to look easy but ... What is a "character"? daz