Bug Vanquisher

16 September 2009

The Off-by-1 Game

Filed under: Bugz, S&G, Tips — Tanveer Badar @ 7:54 PM

Consider this:

for( int i = list.Count – 1 ; i >= list.Count – 50 ; –i , )
.     array [ i ] = list [ i ];

Intention was the get the last 50 terms from list. Ignoring the facts that it will throw a null reference exception if the list is null and an index-out-of-range exception if it contains no elements it also fails to copy all 50 elements. The condition is to be blamed.

Correct way is not to invent your own logic and use what the framework offers.

if( list != null && list.Count > 50 )
    list.RemoveRange( 50 , list.Count – 50 );

For added bonus, consider this paging code.

for( int i = 0 , index = 0 ; i < some_huge_number ; ++i )
{
    page.Results.Add( results [ i ] );
    if( index == 10 )
    {
        pages.Add( page );
        page = new Page( );
        index = 0;
    }
    ++index;
}

This code strives to divide results into pages each with 10 results. Can you imagine what this will do if there were only 9 results?

It will proceed to add those results to page, then, do nothing. Because of the incorrect if condition. Again, the correct way is divide-and-remainder approach as demonstrated here.

Advertisements

When Oracle Met A Computer Architect

Filed under: Bugz, Dev inside!, Tips — Tanveer Badar @ 7:49 PM

Oracle can’t handle more than 1000 expressions in a single in clause. (Really stupid restriction, I know and here’s the tip: you can do (, ,,,,,) or abc in (,,,,,,) ad infinitum).

When compilers software pipeline a loop, they calculate two things at first. The remainder and the dividend.

(This post got too keyword heavy. Matrix fans will curse me that its the other Oracle. That Oracle’s users will not find all ORA-xyz error descriptions here. Architects may not have any interest in writing computer code. Computer architects will not see any processors discussed. One post to rule them all. ]:) )

The problem at hand was hand generating a query with in clause in its where section. Number of expressions was unbounded. And the genius who came up with the loop did this:

for( int i = 0 , n = 0 ; i < count ; ++i )
{
    n++;
    if( n == 1000 )
    {
        quwry += " ) or in ( ";
        n = 0;
    }
    query += list [ i ] + ‘,’;
}
query = query.Remove( query.Length – 1 , 1 );

Remember this is the work of someone who has never heard about software pipeline and may never know what gcc or cl are. Thinking one level above will help us arrive at the correct solution (notice, there is nothing wrong functionally with the above code either. It does what it says it does.):

int n = numbers.Count / stride , k = numbers.Count % stride;
StringBuilder builder = new StringBuilder( );
if( n > 0 )
{
    builder.AppendFormat( "'{0}’" , numbers [ 0 ] );
    for( int j = 1 ; j < stride ; j++ )
        builder.AppendFormat( " , ‘{0}’ " , numbers [ j ] );
    for( int i = 1 ; i < n ; ++i )
    {
        builder.Append( separator );
        builder.AppendFormat( "'{0}’" , numbers [ i * stride ] );
        for( int j = i * stride + 1 ; j < ( i + 1 ) * stride ; j++ )
            builder.AppendFormat( " , ‘{0}’ " , numbers [ j ] );
    }
}
if( k > 0 )
{
    if( n > 0 )
        builder.Append( separator );
    builder.AppendFormat( "'{0}’" , numbers [ n * stride ] );
    for( int j = n * stride + 1 ; j < numbers.Count ; ++j )
        builder.AppendFormat( " , ‘{0}’ " , numbers [ j ] );
}

No repetitive checking of one variable which also displays off-by-1 error. And which is also mission the other correction in first line of this post in bold.

13 September 2009

is This Beautiful?

Filed under: Dev inside!, Rant vs Vent — Tanveer Badar @ 10:01 PM

CodeTypeDeclaration tablerow = new CodeTypeDeclaration
{
    IsClass = true ,
    Name = ( row [ "TABLE_NAME" ] + "Row" ) ,
    IsPartial = true ,
    BaseTypes = { datarowref } ,
    Members =
    {
        new CodeMemberField( ( row [ "TABLE_NAME" ].ToString( ) + "DataTable" ) , "table" + row [ "TABLE_NAME" ] ),
        new CodeConstructor
        {
            Attributes = MemberAttributes.Public ,
            Parameters = { parameter } ,
            BaseConstructorArgs = { buildervar } ,
            Statements =
            {
                new CodeAssignStatement( new CodeFieldReferenceExpression( thisref , "table" + row [ "TABLE_NAME" ] ) , new CodeCastExpression( row [ "TABLE_NAME" ].ToString( ) + "DataTable" , tablefield ) )
            }
        }
    }
};

To some, it may be but for me it is nauseating. What happened to all those .Add and .Insert calls?

CodeDom Fun

Filed under: Dev inside! — Tanveer Badar @ 5:52 PM

Writing CodeDom statements by hand is no fun. Visual studio team members who wrote the code to generate data sets must be the finest. For example, guess what would be the output of this statement:

constructor.Statements.Add( new CodeAssignStatement( new CodeFieldReferenceExpression( new CodeThisReferenceExpression( ) , "table" + row [ "TABLE_NAME" ] ) , new CodeCastExpression( row [ "TABLE_NAME" ].ToString( ) + "DataTable" , new CodeFieldReferenceExpression( new CodeThisReferenceExpression( ) , "Table" ) ) ) );

Just something like

this.tableDT = ( DTDataTable ) (this.Table );

Side note:

I am back!

1 June 2009

Silver Jubilee Week

Filed under: Personal — Tanveer Badar @ 11:58 AM

Its Silver Jubilee Week people. Get ready!!!

24 April 2009

Tales From Interviewer’s Desk

Filed under: Dev inside!, Fun, Tips — Tanveer Badar @ 8:24 PM

Part 1:

How to reverse a string in-place?

Take 1: (Fresh)

string reverse( string str )
{
    char ch = str [ 0 ];
    foreach( int i = 0 ; i < str.length ; ++i )
        Response.RedirectWrite( str [ str.length – i ] ) + ch.tostring( );
}

Take 2: (Big words, no knowledge)

string reverse( object obj )
{
    string str = TypeCaste.tostring( obj );
    stack< ch > s;
    for( int i = 0 ; i < length ; ++i )
        s.push( str.charat( i ) );
    for( i = 0 ; i < length ; ++i )
        str.insertat( 0 , s.pop( ) );
}

Take 3: (2.5  year experience in C#)

string reverse( object obj )
{
    if( typeof( obj ) == "string" )
    {
        for( int i = 0 ; i < obj.length ; ++I )
        {
            char ch = str [ i ];
            str [ i ] = str [ length – i ];
            str [ length – i ] = ch;
        }
    }
    return obj.tostring( );
}

Take 4: (Fresh)

string reverse( object obj )
{
    if( obj == "string" )
    {
        char ch = str [ 0 ];
        for( int i = 0 ; i < obj.length ; ++I )
            str [ i ] = str [ length – i ];
        str [ str.length ] = ch;
    }
}

5 April 2009

Enough!

Filed under: Intelligence :( ?? — Tanveer Badar @ 2:45 PM

Facebook is becoming an information disclosure nightmare. People are too stupid to be handed a public social network for their own good. For example, this just crossed my ridiculously crafted homepage few minutes ago.

"Muhammad just added the birthdays of 15 friends to their MyCalendar.
Muhammad has requested to add the following birthdays to their MyCalendar:"

[more blah blah blah with actual dates]“

My comment:

Tanveer Badar at 14:31 on 05 April
[noun], you should know better. It is information disclosure, and about other people’s personal information you have no right to publish in public.
Please don’t add mine to this application.

What amuses me is that the guy is supposed to be specializing in networking, and security by implication (I think). I shudder to think whose data rests in the hands of such and likes.

Those who take time to read this blog might have noticed there is very little information about me here. You won’t find much about me online because I don’t want you to. But how are you supposed to prevent others leaking information about you online.

28 March 2009

Work Efficiency

Filed under: Bugz — Tanveer Badar @ 7:01 PM

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current Dload  Upload   Total   Spent    Left  Speed
109   219  109   219    0     0    438      0 –:–:– –:–:– –:–:—   438

I like this downloader, given how it built redundancy by downloading 109% of a 219 byte long file.

Work Efficiency

Filed under: Bugz — Tanveer Badar @ 6:22 PM

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current Dload  Upload   Total   Spent    Left  Speed
109   219  109   219    0     0    438      0 –:–:– –:–:– –:–:—   438

I like this downloader, given how it built redundancy by downloading 109% of a 219 byte long file.

26 March 2009

From ntddk.h

Filed under: Dev inside! — Tanveer Badar @ 3:02 AM

//
// This section is specified/returned if the
// ContextFlags word contians the flag CONTEXT_FLOATING_POINT.
//

FLOATING_SAVE_AREA FloatSave;

//
// This section is specified/returned if the
// ContextFlags word contians the flag CONTEXT_SEGMENTS.
//

ULONG   SegGs;
ULONG   SegFs;
ULONG   SegEs;
ULONG   SegDs;

//
// This section is specified/returned if the
// ContextFlags word contians the flag CONTEXT_INTEGER.
//

ULONG   Edi;
ULONG   Esi;
ULONG   Ebx;
ULONG   Edx;
ULONG   Ecx;
ULONG   Eax;

//
// This section is specified/returned if the
// ContextFlags word contians the flag CONTEXT_CONTROL.
//

ULONG   Ebp;
ULONG   Eip;
ULONG   SegCs;              // MUST BE SANITIZED
ULONG   EFlags;             // MUST BE SANITIZED
ULONG   Esp;
ULONG   SegSs;

.
.
.

volatile ULONG DismountCount;

//
// This field indicates the status of the 64-bit COM+ package on the
// system. It indicates whether the Itermediate Language (IL) COM+
// images need to use the 64-bit COM+ runtime or the 32-bit COM+ runtime.
//

Bhahaha!

« Newer PostsOlder Posts »

Create a free website or blog at WordPress.com.