How can I crack you? Let me count the ways…

I just received source code for a partially-implemented Hidden Markov Model. I took one look at the main() (it’s written in C++) and decided I just had to post it here for public review, critique, and general merrymaking.

It’s not even that poorly-written (from aesthetic and functional standpoints, anyway); while it may be “just” an assignment, we all know how code which was never meant to see the light of day ends up becoming the cornerstone for some sort of legacy system that simply…won’t…die. So I offer this as an exercise to count how many different lines you could possibly exploit:

int main(int argc, char *argv[])
{
  Model *m;
  char modFile[300];
  char seqFile[300];
  strcpy(modFile,"");
  strcpy(seqFile,"");
  int action=0;
  int N =2;
  for (int i=1; i<argc; i++) {
    if (!strncmp("-train",argv[i],2)) {
      action = 1;
    } else if (!strncmp("-decode",argv[i],2)) {
      action = 2;
    } else if (!strncmp("-count",argv[i],2)) {
      action = 3;
    } else if (!strncmp("-m",argv[i],2)) {
      if (i+1 >= argc) {
        cerr << " a file name must follow option -m \n";
        exit(1);
      } else {
        strcpy(modFile, argv[i+1]);
        i++;
      }
    } else if (!strncmp("-s",argv[i],2)) {
      if (i+1 >= argc) {
        cerr << " a file name must follow option -s \n";
        exit(1);
      } else {
        strcpy(seqFile, argv[i+1]);
        i++;
      }
    }
  // ... snip ...

I promise a real update is coming soon, once my thesis is done (within the next week).

Advertisements

About Shannon Quinn

Oh hai!
This entry was posted in Programming and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s