﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Ayende Rahien commented on Challenge: Can you spot the bug?</title><description>A string of size 256 with a "\r\n" somewhere in it
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment37</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment37</guid><pubDate>Sun, 15 Nov 2009 02:43:57 GMT</pubDate></item><item><title>Neil Mosafi commented on Challenge: Can you spot the bug?</title><description>So what's the answer I'm curious now!?
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment36</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment36</guid><pubDate>Sat, 14 Nov 2009 15:48:20 GMT</pubDate></item><item><title>Peter commented on Challenge: Can you spot the bug?</title><description>Get creative people! I say Ayende needs to replace what he has with code that calls Regex.Replace successively, each time building on the last Regex.Replace. Incremental design at its best!
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment35</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment35</guid><pubDate>Fri, 23 Oct 2009 05:29:30 GMT</pubDate></item><item><title>meo commented on Challenge: Can you spot the bug?</title><description>Yes, the bug is that on Win Environment.NewLine is 2-bytes, and you replace it with one-byte space, so, for instance, this code (5 line breaks in it):
  
@"
  
  
  
  
  
".FirstCharacters(10);
  
gives you an exception you've listed above.
  
  
M?
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment34</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment34</guid><pubDate>Thu, 22 Oct 2009 14:08:52 GMT</pubDate></item><item><title>Harry commented on Challenge: Can you spot the bug?</title><description>@ Ayende,
  
So, are you saying the possible negative parameters for SubString is not the problem since this routine is not designed for that purpose anyway? If so, I am out of clue why it throws that error. Can you shed some light?
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment33</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment33</guid><pubDate>Thu, 22 Oct 2009 13:45:01 GMT</pubDate></item><item><title>Stephen Lacy commented on Challenge: Can you spot the bug?</title><description>the first problem I would find with the method is that it doesn't describe what it does in it's name.
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment32</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment32</guid><pubDate>Thu, 22 Oct 2009 12:04:35 GMT</pubDate></item><item><title>Jamie commented on Challenge: Can you spot the bug?</title><description>if (self == null)
  
        return "";
  
if(self.Length == 0)
  
        return "";
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment31</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment31</guid><pubDate>Thu, 22 Oct 2009 11:11:48 GMT</pubDate></item><item><title>GarlandGreene commented on Challenge: Can you spot the bug?</title><description>You check for a shorter string length than numOfChars but not for one with the same length (which you could immediately return without change). Another one is that numOfChars could be smaller than 3, which would also cause the same exception as the length parameter for the substring would negative.
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment30</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment30</guid><pubDate>Thu, 22 Oct 2009 10:11:01 GMT</pubDate></item><item><title>Billy Stack commented on Challenge: Can you spot the bug?</title><description>You are missing another pre-condition:
  
  
If(numOfChars &lt;0) { throw ArgumentException(); } or
  
  
If(numOfChars &lt;0) { return "" } 
  
  
depending on desired solution...
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment29</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment29</guid><pubDate>Thu, 22 Oct 2009 09:26:33 GMT</pubDate></item><item><title>Koen commented on Challenge: Can you spot the bug?</title><description>The second parameter of SubString() would be negative if numOfChars == 1 or 2...
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment28</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment28</guid><pubDate>Thu, 22 Oct 2009 08:06:05 GMT</pubDate></item><item><title>Jochen Jonckheere commented on Challenge: Can you spot the bug?</title><description>Well I had Pex running over the code and it has found the ArgumentOutOfRangeException 2 times. Once for numOfChars 0 and once for numOfChars as a negative number.
  
  
Did I cheat, or did I find a use for Pex? ;-)
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment27</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment27</guid><pubDate>Thu, 22 Oct 2009 07:17:16 GMT</pubDate></item><item><title>Eyan commented on Challenge: Can you spot the bug?</title><description>The string length could be less than 3 charachters
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment26</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment26</guid><pubDate>Thu, 22 Oct 2009 06:30:49 GMT</pubDate></item><item><title>Tim Van Wassenhove commented on Challenge: Can you spot the bug?</title><description>Despite the Length &amp; Offset issues, i find something a lot more troublesome: 
  
  
The fact that a method "FirstCharacters" removes spaces and that it appends "..." in some situations is something that i would consider a very naste side-effect of a method that returns the first characters of a string.
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment25</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment25</guid><pubDate>Thu, 22 Oct 2009 06:05:40 GMT</pubDate></item><item><title>Johannes Rudolph commented on Challenge: Can you spot the bug?</title><description>I didn't take time to read the comments, but here are my two cents: 
  
It's preety easy to Spot: You don't check numOfChars &gt; 3
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment24</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment24</guid><pubDate>Thu, 22 Oct 2009 05:19:48 GMT</pubDate></item><item><title>Leaner commented on Challenge: Can you spot the bug?</title><description>For your specific error, this will occur when numOfChars-3 &gt; self.Length after the replacement of the NewLine with " ".  
  
  
This requires that there be 4+ NewLines in the string (and using multibyte NewLine), and numOfChars is between string.Length and (string.Length - numOfNewLine) + 4
  
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment23</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment23</guid><pubDate>Thu, 22 Oct 2009 00:52:25 GMT</pubDate></item><item><title>Steve Py commented on Challenge: Can you spot the bug?</title><description>@ Ayende,
  
  
Well, in all honesty it should be treated as a very generic routine. You are adding it as an extension method to String so it's as public and available as it could possibly be. 
  
You've tripped one bug, but as mentioned by Roberto, there are at least 2 issues in there. (newline replacement reduces the length by 1 for each replacement so it will crash after 4 or more CRLFs, and numOfChars cannot be less than 3.)
  
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment22</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment22</guid><pubDate>Wed, 21 Oct 2009 21:54:31 GMT</pubDate></item><item><title>Mahendra Mavani commented on Challenge: Can you spot the bug?</title><description>Substring(0, numOfChars - 3) will fail for numOfChars=0,1,,2 hence  FirstCharacters(0), FirstCharacters(1),FirstCharacters(2) will give you exception , provided your original string is long enough to bypass first two if conditions 
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment21</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment21</guid><pubDate>Wed, 21 Oct 2009 21:34:34 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: Can you spot the bug?</title><description>I am not quite sure how to respond to that, but I think I'll refer you to my recent posts about unit testing.
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment20</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment20</guid><pubDate>Wed, 21 Oct 2009 21:20:08 GMT</pubDate></item><item><title>Rik Hemsley commented on Challenge: Can you spot the bug?</title><description>In which case the code isn't covered by unit tests... which is the WTF.
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment19</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment19</guid><pubDate>Wed, 21 Oct 2009 21:18:22 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: Can you spot the bug?</title><description>Rik,
  
I am watching the thread going on and growing more &amp; more amused by the second.
  
People are trying to make this into a very generic routine, when in fact it is used only in very specific circumstances.
  
The tests are there for those circumstances, not for generic behavior.
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment18</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment18</guid><pubDate>Wed, 21 Oct 2009 21:16:16 GMT</pubDate></item><item><title>Rik Hemsley commented on Challenge: Can you spot the bug?</title><description>Because it's very easy to make it fail. Or are the tests useless?
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment17</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment17</guid><pubDate>Wed, 21 Oct 2009 21:12:15 GMT</pubDate></item><item><title>Ayende Rahien commented on Challenge: Can you spot the bug?</title><description>Rik,
  
Why do you assume that it isn't covered in tests?
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment16</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment16</guid><pubDate>Wed, 21 Oct 2009 21:05:12 GMT</pubDate></item><item><title>Rik Hemsley commented on Challenge: Can you spot the bug?</title><description>This is exactly the sort of code which is easy to cover with unit tests. The fact it obviously isn't is a big WTF.
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment15</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment15</guid><pubDate>Wed, 21 Oct 2009 21:01:49 GMT</pubDate></item><item><title>Harry commented on Challenge: Can you spot the bug?</title><description>I think Roberto is right for negative substring ...
  
I wrote a test program and to my surprise the following code
  
            Console.WriteLine(@"
  
            asdf
  
  
  
  
  
  
asdf
  
            asdf
  
            asdf
  
            asdf
  
            ".FirstCharacters(10));
  
  
returns "          ..."
  
  
don't know why
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment14</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment14</guid><pubDate>Wed, 21 Oct 2009 21:01:22 GMT</pubDate></item><item><title>Stijn Guillemyn commented on Challenge: Can you spot the bug?</title><description>On a side note, I also don't like some things about the implementation in general.
  
  
For instance that a null reference returns an empty string. I'd prefer to see it return null instead.
  
Also, if the length == numOfChars, why don't you just return the string? It's not too long in this case.
  
  
@Roberto: I think it's not that much the length of the string that should be greater than 3. Rather than the length of the ellipsis that should depend on numOfChars. If someone requests numOfChars == 1, you cannot add an ellipsis of 3 (and take it in account for your substring). The requested numOfChars should be &gt; 3 for this implementation to work.
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment13</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment13</guid><pubDate>Wed, 21 Oct 2009 20:58:02 GMT</pubDate></item><item><title>Stijn Guillemyn commented on Challenge: Can you spot the bug?</title><description>Although most bugs were already found:
  
  
- Replacing newline after checking string length
  
- The offset of 3 for the ellipsis isn't taken into account when checking the length
  
  
I think I've got another one. Nothing prevents the method to be called with a negative int, isn't it? This would also blow up, I guess.
  
Safer to use an unsigned int for the number of characters?
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment12</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment12</guid><pubDate>Wed, 21 Oct 2009 20:41:37 GMT</pubDate></item><item><title>tobi commented on Challenge: Can you spot the bug?</title><description>the solutions mentioned so far were pretty easy to spot so i guess it was more complex than that.
  
  
a) when there are many new lines the second parameter is larger than the length of the string.
  
b) when num of chars was less than 3 the length was negative
  
  
here is a proof that no other explanation can exist. from the error message we know that the substring call is the culprit. as the first argument is an always valid constant the second argument must be the reason. it can either be &lt; 0 or &gt; string.length. so a) and b) are exhaustive.
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment11</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment11</guid><pubDate>Wed, 21 Oct 2009 20:39:44 GMT</pubDate></item><item><title>Kyle Szklenski commented on Challenge: Can you spot the bug?</title><description>Roberto, SubString accepts 0 as its range; it simply takes no characters.
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment10</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment10</guid><pubDate>Wed, 21 Oct 2009 20:29:55 GMT</pubDate></item><item><title>devdimi commented on Challenge: Can you spot the bug?</title><description>" ". FirstCharacters(1);
  
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment9</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment9</guid><pubDate>Wed, 21 Oct 2009 19:49:55 GMT</pubDate></item><item><title>Roberto commented on Challenge: Can you spot the bug?</title><description>I know it... i get it everytime :D
  
  
you didn't check if the actual string length is greater than (or egual to) 3
  
  
substring can't manage a negative range (0 to -1/-2/-3, in that example)
  
  
in the length check, you have to put too another expression, if lenght is greater than 3  (and maybe use a constant for this number)
</description><link>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment8</link><guid>http://ayende.com/4258/challenge-can-you-spot-the-bug#comment8</guid><pubDate>Wed, 21 Oct 2009 19:47:26 GMT</pubDate></item></channel></rss>