Archive for the 'Programming' Category

Try Sparse

Try Sparse to analyze your C code. Unlike Splint, it works well with the GTK libraries and reports a wide range of issues. It also neatly fits in with the autotools build system, so you can basically run your autotools and prep for a make, and then call:

make CC=cgcc

cgcc is a wrapper which invokes sparse first, and then gcc. Then go through the warnings and fix them. :)

tinyproxy is back

The last maintainer of tinyproxy transferred over maintainership of the project yesterday. So the fork nanoproxy is now obsolete and has been removed. The tinyproxy project will keep its old name, because existing users know it as that.

Visit the tinyproxy project!

Forked tinyproxy

I forked tinyproxy to create nanoproxy today.

I’ve been a regular user of tinyproxy for the past 2 years, as it allows me to masquerade as a user in another country/university pretty easily through a SSH shell account. Unfortunately, tinyproxy has been unmaintained for about 3 years now and is marked as inactive on SourceForge.net. There are known bugs in it. I also want it to be a part of my Linux distribution. So what’s better than to maintain it. I tried to contact the last active author to ask for access to maintain it, but didn’t get any responses so I forked it to a new project called nanoproxy. There’ll be releases after a few things are fixed.

GIMP 2.4 is out!

GIMP 2.4 has been released! It is the culmination of many man-years of real effort by several developers from around the world. There are many new things to expect in the new version, with more features, better usability and less bugs. Uncork your champagne and enjoy by upgrading to it.

Splint and variadic macros

It would be very worthwhile for someone to add support for variadic macros to Splint (a C static analysis tool similar to lint but better). While it is not as good as Coverity Prevent and generates several false positives, it can be made to work with GLib/GTK+ application code and would be very helpful in eliminating several types of programming errors. It is certainly much better than lint. It has been used on GIMP trunk, along with other static analysis tools and it found errors which were fixed. Another tool called href=”http://www.cubewano.org/oink”>Oink doesn’t compile on my x86_64 box, but seems interesting nevertheless as it claims to support C++.

I dream of a day when C/C++ programmer editors do static analysis as I type, and suggest corrections to stupid mistakes. NetBeans (Java) has really spoiled me, by how productive one can be using it.

Subversion and merging

Subversion’s lack of good merging support is proving to be difficult. Over the last 2 weeks, my colleagues and I have been bitten by at least the following issues (perhaps due to our arrangement of branches which make these issues conspicuous):

  • The multiple merge problem which is documented in the Subversion manual: create a branch and when you merge from one to another more than once, you have to 1. record the number of the revision you’re merging upto in the commit log, and 2. supply this revision number when you merge the next time. Otherwise, if you have changed some lines that were brought in when you merged last, you will get conflicts. And be careful when recording the point you merged till if you are looking up its revision number again, as the from-branch may have more revisions since you merged. Cherry picking is also not possible unless you track every changeset you merged.
  • You may have to resolve the same conflict again when merging into a working copy from your colleauge’s branch, even if your colleaugue already merged your latest changes, picked his change over yours in that conflict in his working copy and committed this into his branch. Subversion does not seem to be able to record and use this info.
  • If multiple developers merge into their checkout of trunk and commit into trunk, from their private/topic branches in an office environment where merging can happen often, these merges may happen at nearby times. In some of these cases, you may reverse out some changes of your colleagues without any warning even though you ran svn up before you committed. When I hit this issue, I started wondering if this was the reason for Git’s index (I never quite followed the need for it).

What is this code?

char*a=" $#,.';:nt",*s="�000at�31r1�20�21�05",*d="215266333
S3�32�04321232L32176233346;346�30J~G200317251373O�01237h
3371C233317211274317371�23364235�33314377221276a360371
f~341342303326361277353~O207307h337365277\"�34o304ygb
3146317333|�33355�03�16�333063773411264�04375�336202a
242~3061",i,j,r=0,b=0;main(){for(i=0;i<98;i++){for(;;){r<<=1;r|=(*(d+i)
>>(7-b++))&1;for(j=0;j<10;j++) if(*(s+j)==r){putchar(*(a+j));r=0;break;}
if(b>=8){b=0;break;}}}}

Eek! Look at all that terrible code. Anyway, main() implements a well-known algorithm. Can you figure out what it is?

DH2C.COM

When I was pursuing a B.Sc. degree in Loyola Academy about 11 years ago, we used DOS and diskless machines with floppies. There was a pretty terrible virus called Die Hard 2 doing the rounds in the labs. We didn’t have a cleaner utility for this virus, and even if the McAfee SCAN.EXE and CLEAN.EXE that we had could detect and clean it (which they couldn’t anyway), they were way too slow to run. Simply loading either off a floppy into memory and program setup took a minute on those 8088 machines and then scanning took what seemed to be forever. It was during this time when I was getting better at assembly and also going through virus disassembly :) . I wrote a program called DH2C.COM (Die Hard 2 cleaner) in 8086 assembly which got used a lot on campus for its high speed, so much so that some people changed strings in it using a hex editor and called it their own. You could clean an entire disk in less time than SCAN.EXE took to load. This was due to DH2C’s use of file truncation and very little disk reading to check for infection.

Here is the program DH2C.COM. I seem to have lost the original source code over the years, but here is a disassembly of it. I don’t know if I can release this disassembler-generated code as free software, but the COM file is released under the modified BSD license or GNU GPLv2 and higher (no warranty, no liability). If time permits, I will comment it in the future but it should be fairly straightforward to anyone who has done DOS programming.

Three nutty people wracked their heads to come up this

It’s because the % operation is implementation defined over negative integers in C. It was originally written by nomis, pippin, and me for a GIMP patch many moons ago, but we decided not to put the patch in. As this code took a while to arrive at, here it is for your pleasure and so that we may never have to rewrite it.

#define REMAINDER(dividend, divisor)
                   (((dividend) < 0) ?
                    (divisor) - 1 - ((-(dividend) - 1) % (divisor)) :
                    (dividend) % (divisor))

Update: The macro assumes a positive divisor. Our divisor was always a constant positive integer literal.

Morten Welinder found an underflow situation in the code above when INT_MIN is used as the dividend, which may fall in yet another undefined part of the C specification. Basically in the sub-expression (-(dividend) - 1), -(dividend) == (dividend) when dividend == INT_MIN. And INT_MIN - 1 will underflow (and wrap-around to INT_MAX in the case of most microprocessors’ two’s complement registers), and this behaviour is not defined in C. Although it’ll work fine on most general purpose CPUs, because this is undocumented, there may be issues if gcc tries to reduce it at -O2 if both dividend and divisor are constants. More correct code with Morten’s patch is:

#define REMAINDER(dividend, divisor)
                   (((dividend) < 0) ?
                    (divisor) - 1 - ((-((dividend) + 1)) % (divisor)) :
                    (dividend) % (divisor))

It won’t overflow over INT_MAX in this case, as dividend is less than 0 when that sub-expression is used. So now, we have 4 authors. ;)

Gum update

Following last week’s post on Gum (free software for DRM-free media distribution to refresh your memory), I have received some suggestions from various people. One of them was to try and build a community around Gum. So I’ve made a project website for Gum (as it is free software) which lists things to do, access to source code, etc. and I’ve setup a mailing list and IRC channel (#gum on irc.freenode.net). Everyone is welcome to participate. :) I’ll upload whatever source code is done till now to the Subversion repository in the morning tomorrow.

In the past week after the post, I did some database development for the backend. I also contacted Magnatune on the phone and and they have kindly agreed to have their whole catalogue available on Gum under CC licenses (it was free, but I had better ask them before using such a large catalogue, no?). It was a very nice gesture from them, and their data is in various tables now, helping with database testing and query optimizations. A rudimentary web interface is also done for audio items which shows metadata (you can find it if you know where to look ;) ). More things are in the works.

I re-request help for Gum. If any software company can sponsor salary, hardware and bandwidth, I can work full-time on this project and perhaps get a full-time co-developer or two. There are many items to do. It will definitely rock to have funding. Nokia, Red Hat, Novell, Sun.. are any of you interested? It can’t hurt much to hire one guy to work on DRM-free media distribution software and your desktop/handheld OS gets a nice GTK+ application. Well everyone does.. that’s the beauty of free software.