The horror! Scientific code and how not to read your arguments…

Over the years I have seen many, many examples of poor programming practise, usually kludges and quick fixes but today I saw the most horrible code for reading in command-line arguments in a C program ever. I just had to share the horror…

   if ( (argc-1) < 5 ) {
	.
	.
	.
	[ Usage error response code removed]
	.
	.
	.
   }

   /* read in command-line arguments */
           
   numFiles = (argc-1) - 6;
   sscanf( argv[ numFiles+1 ], "%s", insFileName );
   sscanf( argv[ numFiles+2 ], "%s", outFileName );
   sscanf( argv[ numFiles+3 ], "%d", &outType );
   sscanf( argv[ numFiles+4 ], "%hd", &windowStartTimeCodeword0 );
   sscanf( argv[ numFiles+5 ], "%d", &newStartLine );
   sscanf( argv[ numFiles+6 ], "%d", &newEndLine);

Now, where can I start with this? Erm, I’m a bit dumbfounded actually.

Not only does the test for the incorrect number of arguments test for the wrong number but then it uses an index from the last value to reference the other values! Of course, this means that if the wrong numbers of arguments are given then the values are put into the wrong variables. Worse, that could be read from memory the process doesn’t own.

And there’s more.. it blindly sscanf()s them into variables.

Now, you may have seen that if one argument is left off the command line the input file now becomes the executable itself and the output file is actually the input data file. This is how this came to my attention. Trying to debug the program for a student it was found that it wasn’t reading the data correctly… and the data file was mysteriously emptied of its hundreds of megabytes of data each time the program was run. Oops!

So, dear readers, have any of you ever seen a worse command line parsing code segment?

IPv4 addresses almost gone, IPv6 not finished yet. Oops!

As has been noted very widely the last couple of large blocks of Internet Protocol version 4 addresses have been assigned to the local distributors and rightly there have been a large number of people stating that we need to get ready for the transition for IP version 6.

However, there are a few niggly little problems due partly to do with IPv6’s design and partly by tardy implementation, neither of which impact upon the general public and their edge networks but will impact upon the security and management of more corporate networks.

So, what are these two problems? Well, they’re both to do with network address assignment, one of which is a foolish design decision in the protocol itself which has a whole host of unintended consequences related to it.

The feature I’m talking about here is the stateless address assignment where a client machine will self-assign its address and self-discover the route out to the wider Internet. On the face of it it seems like a brilliant idea which will liberate the normal user from worrying about setting up IP addresses and all that tedious and confusing networking stuff, it all “just works”. Brilliant! And, in a perfect world, where everyone is smiley, helpful and trustworthy it would be. It’s a pity that the real world isn’t like that. Having said that, this doesn’t really affect personal networking within peoples’ homes but it does greatly affect the security and policing of corporate networks.

At this point it’s probably best to describe how security and policy are implemented, with regards to network addresses and packet routing in IPv4 networks so as to allow you to contrast the differences and the problems inherent in the self-assigned address world of IPv6. Currently a computer can either be manually assigned an address and network route which then has to be configured directly on the computer in question or it can be assigned automatically from a centrally managed Dynamic Host Configuration Protocol (DHCP) server. In the latter case it’s not only the network address and route information which can be given to the computer but other information such as its host name and various other items which it can use to interact correctly with the rest of the network. The centrally managed DHCP server can also tell any computer it doesn’t know (or the administrators don’t want to have network access) to bog off and hence not get network access. Using this very useful system administrators can assign different outgoing network routes for different sets of client machines which can help with load balancing and various other advantageous policies that only humans with an overview of the whole network can see.

As you can see, IPv6’s self-assignment of addresses and self-discovery of network routes by-passes all this control. If you add to this certain client operating systems being “helpful” and offering network tunnels out of the current network for IPv6 clients to the outside world and offering their services as routers it becomes a security nightmare as local outgoing firewall policies and protections are subverted.

Now, this problem has been foreseen, if belatedly, by a group who have, against the uproar of the IPv6 purists, defined an IPv6 version of DHCP. (Note: the purists hate it because it breaks their ideological tenet that all network peers should be equal and free to do as they wish.)

So, surely this means that IPv6 is ready? Erm, no. You see DHCPv6 is only currently a paper exercise. The technical details have been hammered out and the specification documents (RFCs) have been posted but there are no implementations out there. Ooops!

So, what does this mean for the whole IPv4 to IPv6 transition? Well, it means that internal corporate networks will not be able to change to the new protocol and will be forced to live behind an IPv4 to IPv6 network address translation (NAT) gateway. (Note 2: IPv6 purists cringe even more about this technology, they see NAT as the spawn of the devil as it stops all peers being equal and being able to talk directly with every other.)

I can foresee the transition from IPv4 to IPv6 being a long one with to start with only those machines which live in the no-mans-land where external services live and the core Internet changing over to IPv6 and everything else being behind huge NAT gateways. Internet Service Providers (ISPs), whose customers don’t generally have fixed network addresses anyway, will sit all their customers in IPv4 bubbles and this state of affairs will ossify. All web sites will be forced to use IPv4 compatible addresses.

Eventually, after many years, all the tools and security issues with IPv6 will be sorted out and slowly, very slowly, the corporate world will change their networks one by one, but there will always be “legacy” IPv4 networks in there, well at least for 20 years or so. For ISPs the transition will be quicker. They’ll probably have to begin with a separate product for IPv6 users or merely provide IPv6 gateway routers to new customers (quite probably to begin with using an IPv4 NAT bubble for the home network as quite a bit of embedded A/V equipment will not be IPv6 capable). I can foresee that even this transition will take a good decade. During this time all web servers will have to be on IPv4 mappable addresses.

It’s going to be a very long haul and expect things to break horribly.

Why, if time travel were invented today, it would be pointless and deadly.

I know it’s a random subject but bear with me. This is a little thought experiment on the subject of time travel and, if it were miraculously invented today, would be totally pointless and deadly, at least for humans.

First of all, let’s look at what I mean by “time travel”. Basically, my definition is that it is travel forwards of backwards in the fourth dimension of time by popping out of space-time in one temporal location and popping back in another. i.e. there is no spacial movement. This assumes that there is actually a fundamental space reference frame within the Universe which would probably change size and shape caused inflation and local gravitational field effects.

So, why do I postulate that it would be pointless today?

Well, let’s think about this. We are not stationary. We have velocity relative to everything except the few things around us which happen to be enjoying the same small area as we are. Even objects you can see a few feet away from you have a very slightly different trajectory to you because of the curvature of the Earth and its spin and the orbit of the planet around the Sun and also the orbit of the Solar System around the galaxy. So, if you were to just pop out of space-time just for a few minutes you’d find yourself either high in the sky, possibly even in space, travelling in an awkward direction or, even worse, suddenly underground squished by the planet you’ve hit at a couple of thousand miles per hour. Ooops! Paraphrasing Douglas Adams, not a naturally tenable position for a human.

This means, of course, that before you can use your new time-travel machine first you need to build yourself a nice inter-planetary capable space craft. At least you’d not have to worry about getting off the planet but you would have to make sure that your time jump was long enough to make sure nothing was in the way when you popped back into reality.

Well, this would be fine as long as you had a decent inter-planetary drive and enough time to travel back to Earth. The problem comes when you want to travel for more than just a few hours, or maybe days. You see, the further you travel backwards or forwards in time the further you will be from Earth when you arrive. After a few days you’ll be finding yourself popping back into existence outside the Solar System so you’ll need not just a fast inter-planetary ship but an inter-stellar one. If a fast inter-planetary craft is hard to come by at the moment then a nice inter-stellar one is even rarer.

Of course, there are other problems when you start moving very far from where you began, the most notable of which would be the change in the distortion of space-time due to gravitational field differences. Goodness knows what the effect would be when popping out of the high gravitational field distortion close to the Sun, Moon and Earth and then popping into a far “flatter” field in inter-stellar space. Would the instant change in your own space-time bubble be detrimental to your survival or physical integrity? I don’t know. Of course, this effect would be amplified greatly if you made large time jumps as space-time itself would have changed size due to the expansion of the Universe.

Overall then, time travel, other than the usual one second per second forward, is probably not a good idea, and likely to be a tad unhealthy at that. It’s probably why you’ve not met any time travellers yet.

White (or rather blue) van man damage.

It’s not been a good week for my car. Sometime on Wednesday, whilst it was sitting on my drive, a good 10ft (3m) from the entrance it was hit by another vehicle. It was little more than a rub but it has caused some paint damage and also subtle denting to the rear, driver’s side wheel arch and passenger door. This is a right pain.

From the paint left behind from the other vehicle and the other evidence it looks like it was a dark blue (the same sort of colour as “Home Delivery Network” use on their vans) and the height of the bumper was that of a van. From the angle of the impact it seems that the vehicle was using the wide driveway as a turning place.

The worst thing about it all is that he/she didn’t stop and leave their details. Still, it’s definitely not worth claiming on the insurance as for the next five years I’d be paying far more than any potential repair cost, even though there is no way that it could have been my fault. Such is the way that insurance companies gauge risk.

As I said, what a pain.

BBC News: Sensational and missing the real point.

Having watched a news item on the BBC Ten O’Clock News about the chemical sludge leak in Hungary (www.bbc.co.uk/news/world-europe-11504980) I became quite annoyed by the sensationalist and wholly inaccurate reporting of the toxic dangers inherent from the torrent of contaminants.

The first part of the description of the ingredients was fine, it said that the main component was iron oxide, which was clarified as being rust. After that it became a scare story. You can see by the descriptions in the graphic describing the chemical breakdown of the sludge in the article referenced above how bad it was.

But wait a minute! Let’s look at this information more carefully and remind ourselves that this is a fully hydrated environment as well. I’ll take the ingredients one at a time:

  1. Iron Oxide: Annoying but harmless.
  2. Aluminium Oxide (Al2O3): Well, reasonably inert. There is actually a possible link with dementia with long-term ingestion. Nothing like Aluminium Sulphate, which was what caused the Camelford incident.
  3. Silicon Dioxide: Wait a minute.. note the asterisk. The footnote says that actually it’s NOT silica (i.e. quartz), it’s as part of sodium and aluminium silicates. That’ll be clay minerals then.
  4. Calcium Oxide: Hmm.. Quicklime, in a hydrated environment?! Doh! No, plain old lime, Calcium Hydroxide. Not good for plants but not exactly the worst contaminant in the world. In the environment it’ll probably react with CO2 in the atmosphere and become that well known toxin, calcite… limestone.
  5. Titanium Oxide: That well known toxin used to cover mints and other sweets.
  6. Oxygen-bonded sodium oxide: Hmm.. so, it’s not just an oxide, it’s extra oxygen bonded!!! Oh dear.. epic fail number two. In water this would quickly become NaOH. A bit horrid… will turn fat into soap. A slippery customer. Not a long-term problem though.

Now, what was not mentioned in the TV item and is relatively marginalised in the written piece is the problem of heavy metal contamination. This IS the news worthy part of the contamination problem and it’s been almost totally missed by the reporter. The problem, I think, is that because the concentrations in the sludge are low relative to those headline grabbing amounts handed to the reporter in a press release from the chemical company they don’t sound important to a layman. However, they are the long-term health legacy items.

Mentioned in the written article are mercury and arsenic. I’m very much surprised that cadmium isn’t mentioned seeing as these are essentially mine tailings by proxy.

So, all in all a rather big failure on the part of the reporter who, because of a lack of scientific knowledge, not only made an arse of himself and the BBC but also missed the whole real news story.

Astrophotography, the beginning.

As mentioned in a previous post, I’m recently bought a new telescope with the thought of using it for astrophotography.

Now, seeing as the last part of the order turned up a little over a week ago, the equatorial wedge, I’m finally in a position to start on the project in earnest. To this end I went out shopping on Saturday for a camera and a machine to drive the camera and the ‘scope.

Having read quite a great deal and seen images which other people have managed to take, I opted for the Nikon D90 as the camera body to obtain. Currently, it’s probably the best time to get one of these cameras as it is about to be replaced by the D7000 and hence the street price is at its lowest. Waiting for the D7000 to appear would be counter productive as it will not only be at a premium price which is almost double that of the D90 but also doesn’t have anything really to help with the task at hand. (The main improvements over the D90 are in the realms of auto-focus and a more robust, and heavier, body.)

Unfortunately, because it is about to be replaced it is becoming scarcer, which meant that I could only find it as part of a kit with a lens I don’t actually need. Oh well.

On the control machine front, I picked up a netbook. The twin-core, 1.6GHz Atom processor powered Acer Aspire-One 533 should be ample for the task. There’s not a great deal of processing power required to control the camera, acquire the images or run Stellarium or similar to drive the ‘scope. I just need to get a serial cable now.

Anyway, Saturday evening was a beautiful evening with crystal clear skies, which gave me a good opportunity to have a first experiment.

The results? Well, OK for a first attempt. It’s very difficult to get a good focus when looking though the viewfinder. My first target was the Moon:

moon1

Not too bad for a first attempt…. Then over to the Dumbell Nebula:

dumbell1

As you can see, this 60 second exposure really shows up the problem with light pollution in the area I live. I’m going to have to look into getting a light pollution filter.

Finally, I spent a little while trying to image Jupiter. The best I managed was this:

jupiter1

I’m definitely going to have to look into ways of getting a sharper focus. Not all of the fuzziness is caused by atmospheric disturbance.

Anyway, following on from my photographic exploits I noticed one of the Jovian moons, Io, coming out from behind the planet. It’s amazing but you can actually see it move relative to the planet with the gap visibly changing in only seconds. It must be moving at quite a speed.

Still, I’m not likely to get another chance to play in the near future, looking at the weather forecast. The next items on the shopping list: landscaping with paving and a rolling roof observatory such as this:

observatory
observatory

The end of an era, or should that be epoch?

Yesterday was the last day of the move of the University of Oxford Department of Earth Sciences from the building(s) it’s occupied since 1948 to the new building just around the corner. (The chemistry labs are still situated there until November but the building is now technically “owned” by Chemistry.)

This was the end of an era for me as well. I was the first person to move into the extension in late 1992, getting key number 139, and my office was one of the last to get moved out as well. So, I’ve certainly been the longest resident. Yesterday I handed that key back for the very last time, after having it in my pocket continuously for 18 years almost to the day. The end of another era.

The new building is very different from the old one. It’s light and airy but also more “corporate” and soulless. One real benefit is, however, that the common room is on the roof and last night, at the weekly Happy Hour, it was very pleasant sitting on a sofa with only the spot lights switched on watching the nearly full Moon climb above Headington Hill intermittently shrouded by scudding, silver-rimmed clouds and the sky changing from a light turquoise to a dusty dark blue over the period of an hour or so.

Still, the only constant in life is change and I can see that the new building *IS* an improvement in many ways from the old one. There are things the old building had which were better, such as a larger library, but on the whole there are fewer of those than than the new one’s advantages. Let’s just see how things progress…

 

A tale of two telescopes.

I’ve just taken delivery of a new Meade 8″ LX90-SC telescope and tonight had the first chance to take it for a spin, as it were. This is despite the British weather trying its best to get in the way. Still, the breaks in the cloud were big enough for me to align the ‘scope.

I have been surprised by the contrast between this ‘scope and the one I bought for my Dad the Christmas before last, however. That ‘scope seemed to be a Friday afternoon job. It arrived with the wrong type of Autostar controller and the battery boxes weren’t (and still aren’t) connected properly. Also, it was far harder to get it to align properly and never seemed to do so accurately enough. His ‘scope is a 10″ Meade LX90-ACF (i.e. has the more expensive optics).

My new ‘scope, on the other hand, seems far better put together. Other than a slightly bent bolt on the tripod, which is used to hold the ‘scope on the tripod, everything is in good working order. Not only this but the alignment process actually works and works well. I think this is partly helped by the replacement of a electronic compass and level (which never seemed to work) with a good, old-fashioned compass with a bubble.

Just to show how good the alignment and calibration was, I set the ‘scope to point at Jupiter about 10 minutes before it rose above the roof of a near-by house. (It also happened to be in cloud as well, but that’s beside the point.) When it did appear it was right in the centre of the field of view. I didn’t need to tweak it at all. This never was possible with my Dad’s ‘scope.

Also, the image was a great deal better than with my Dad’s ‘scope, and the magnification seemed higher (with the same type of eye-piece), even though they’re supposed to have the same focal length. This was very unexpected as the ‘scope is smaller and has the cheaper optics.

Am I happy? Yes. Other than the minor problem with the bolt, which makes it slightly more difficult to set the kit up and dismantle it again, it’s actually better than I expected.

 

In a spin: Speed cameras and the fallacy of road safety.

Recently, with the cut in funding and subsequent turning off of speed cameras in Oxfordshire there has been a great deal of chatter about how this will increase the numbers of deaths on the roads. Today had the local speed camera organisation, “Thames Valley’s Safer Road Partnership”, spouting statistically unsustainable statistics about how many people were now breaking the speed limit as opposed to when the cameras were operational. (Notably, they didn’t say by how much the speed limit was being broken by. Also, one location is notorious for chavish lads driving recklessly, which wouldn’t be halted by the camera.) www.bbc.co.uk/news/uk-england-oxfordshire-10929488

Two days ago we had another pressure group and a number of police chief constables put their oar in: www.bbc.co.uk/news/uk-10911436. One basically saying that there would be carmogedden and the other blaming the middle classes for speeding “until one of their family got killed.” That second one is laughable if you’ve ever lived anywhere close to a “working class” neighbourhood.

The trouble with all this spin and hoo-hah is that the evidence for speed being the main killer on the roads is just not there. In 2008 only 14% of road accidents had speed as a contributory factor. www.dft.gov.uk/pgr/statistics/datatablespublications/accidents/casualtiesgbar/rrcgb2008 Note that this is ALL accidents reported to the police and not just lethal ones. So, speed cameras, even if they were 100% effective, would not help in 86% of accidents.

So, now we know that speed is not as high a risk as the quangos who run the cameras and the police forces (who don’t want to spend money on traffic enforcement officers) would make out, do cameras actually help at all?

Well, in some locations I’m sure that they do. In most locations, however, they’re more of a hazard than a help. How many times have you seen a car brake heavily (usually from a speed under the speed limit already) to pass a speed camera? I’ve seen it quite a number of times, causing the cars behind to have to make sudden braking maneuvers themselves. i.e. increasing the risk of an accident. Also, forcing drivers to be more concerned about the absolute speed of their vehicle and watching the speedo more and more decreases the time they have to concentrate on other road hazards or taking note of what an appropriate speed for the situation might be, again increasing the risk of an accident.

If you add to this the congestive effect of cars slowing to well below the speed limit to pass the cameras and, on a road close to capacity, causing the generation of a solitary wave of congestion to pass back along the queue causing a complete jam.

What about the other “safety” features imposed by local authorities on the roads in the name of safety?

Well, there are the extensive 50 mph speed limits which seem to encompass huge swathes of the A road network. It seems that the default reaction when there are an above average number of accidents on a road is to restrict the speed limit by a mere 10 mph. The fallacy here is that doing so will automatically cut incidents.

Let’s break down the logic using a specific example, the A420 between Oxford and Swindon. There are a few places where the junctions are relatively blind and would benefit from the traffic being slowed. It could be said that in these areas 50 mph is still way too high and a 40 mph limit would be more appropriate in the close vicinity of the junction. However, for most of the road the national speed limit of 60 mph is not a dangerous speed (under good road conditions). The majority of fatal accidents on the road, however, are from drivers who have been reckless and have been speeding, losing control of their vehicle on bends. Note: these people are already breaking the speed limit.

So, the knee-jerk reaction has been to decrease the speed limit on about two-thirds of the whole length of road to 50 mph. Firstly, his will not help to greatly reduce the danger on the risky junctions as the time to see a vehicle from the junction before turning out onto the road has not decreased by much. Secondly, those who caused accidents by speeding recklessly will still ignore the speed limit. (And no, more speed cameras wouldn’t help.)

In other words, the responsible majority have been hugely inconvenienced and no real good has come from it other than to allow the councils to say that they’re doing something.

Of course, I am talking heresy. Tut-tut. The slogan says, “Speed kills!” but actually it doesn’t. Changing the slogan to “Inappropriate speed is highly risky” wouldn’t go down well as it’s not punchy enough. Of course, it’s not the speed which kills ultimately, it’s the rapid deceleration.

Just my two-penny’s worth…

The iPhone 4 and Bluetooth keyboard… It works!

Well, I’ve been testing out the usability of a ThinkOutside Bluetooth keyboard which I bought originally to go with my Nokia N800 with the Apple iPhone 4.

Well, as you can see, it does actually work and works well.

If you add a proper keyboard to the high resolution of the iPhone 4’s display then it actually makes for a decent “on the go” blog updating device.

Well, it would if the WYSIWYG editor plug-in for WordPress would actually work within Safari, which it doesn’t. This means that you are forced to revert to the WordPress application and its insistence on the document being prepared in plain HTML. Not (non-technical) user friendly at all. (And a bit of a pain even if you are technically minded.)

By the way, to prove that it’s possible to do it, this article has been written entirely on the iPhone 4 + ThinkOutside keyboard combination.

Oh, and one very useful thing is that the “web application” key on the keyboard emulates the big button at the bottom of the iPhone.