{C++ etc.}

Latest Posts
 
Home | C++ Posts | Linux Posts | Programming Posts | Issue Tracking Posts

Sunday, October 11, 2009

Functions that take length of strings as parameters

Lately, I've been working on a component which had HUGE issue with string lengths. The problem was that all the strings were initialized with an integer instead of defined lengths. When these were used in various places in the source, the programmer has used various lengths to alter the same string. And now I find myself faced with the nightmare of cleaning up the mess.

The code used to look like this:
z_User[31];
pCursor->Define(3, z_User, 30); // wrong. would only copy 29 chars off a database column
pMsg->Get(5, z_User, 30); // wrong. would only copy 29 chars off a message
ToUpperCase(z_User, 30); // correct. expects user to pass the length to convert and then appends '\0'

The problem is, functions which take string length as an argument would treat this length differently. For example, strcpy requires you to give one less than the actual length of the string in order to have space to print the NULL termination character. Another problem is the situation one would find ones self in if the size of the string were to be changed (e.g. change the size of User to 50).
When writing a function which uses char buffs, it would always be prudent to implement it in a safe way so that users would not have to worry about the actual implementation and wonder how it would behave if you pass the full length of the string to it. The above code would ideally be written as (keeping the same structure):

const int LEN_USER = 51;
z_User[LEN_USER];
pCursor->Define(3, z_User, LEN_USER);
pMsg->Get(5, z_User, LEN_USER);
ToUpperCase(z_User, LEN_USER); // Function changed to accept the full length of the string

The real fun begins when you have to do these changes in multiple code paths. :)

Wednesday, September 09, 2009

Installing Shrew vpn client on Fedora 10



There are some dependencies which have to be addressed when trying to install Shrew vpn client on Fedora 10.

IMPORTANT: Try to obtain the latest beta version of shrew. The stable package requires an outdated version of QT (3.2) and it's hard to set up since it uses the config.h file which is not included in modern versions of the linux kernel.
  • yum -y install cmake
  • yum -y install openssl-devel
  • yum -y install flex
  • yum -y install bison
  • install QT dev package using package manager
Run:
cmake -DCMAKE_INSTALL_PREFIX=/usr -DQTGUI=YES -DETCDIR=/etc -DNATT=YES

After installing the shrew client, you could set up the connection to a Cisco VPN network by importing a valid .pcf file. This is preferred over manual set up since the user might not be aware of some of the connection details. If you connect to the VPN using windows, this file is automatically created and you could find it by doing a simple search.

You could use rdesktop as a Remote Desktop client along with shrew to access windows machines in the VPN.


Sunday, September 06, 2009

Remove Stray Code Lines Before a Code Review




One of the modules that I'm in charge of, had an object which set a bool called b_Delete inside the destructor. While working on a new class which would handle a collection of these objects, I used:
list_A.erase(pX); if (! pX->b_Delete) // Already deleted { delete pX; } This was meant to crash if some other class removed the object in an asynchronous callback. After I have removed all the unwanted deletes else where I could remove the check. Unfortunately, the code remained as is. Right now I'm suffering the consequences. Some of my colleagues who saw this think that I'm not aware that you can't refer to objects inside a deleted object. The code works because I've already removed all of the unwanted delete calls on pX. Note to self: Go through each new code line before a code review

Fedora 10 doesn't have gcc pre-installed

Imagine my surprise when I tried to compile a test c++ program after installing Fedora 10 and it failed with the error "Command not found".
I had to install it manually with yum -y install gcc sudo (as root)

Thursday, May 21, 2009

Altering the primary key of an Oracle Table

Eg: Table T_1 has the following columns:
T_1
--------------
NAME VARCHAR2(100) not null (primary key)
AGE INT
ID VARCHAR2(100)
PASS VARCHAR

NAME is the primary key.
If we want to add INT to the primary key and make it a composite key:
1. We must first make the ID column "not null" which means it could not have any NULL entries.
2. We should drop the existing primary key.
3. Create the new primary key.


PL/SQL script to achive this:

alter table T_1 modify NAME VARCHAR2(100) NOT NULL;
alter table T_1 drop primary key;
alter table T_1 add primary key (NAME, ID);
commit;

:)