Prev - Index

Source code for the GDB example

This is a sample C program that contains a number of bugs: some minor, some major. It does nothing useful except help demonstrate GDB.

/* cipher.c
 * A very basic encryption program that no-one in their right mind would use!
 * This is a demonstration program with a nasty bug to demonstrate the
 * essential GDB commands
 * NOTE: there is not for serious use - it is bug-ridden and 
 *       non-functional to boot!
 * Written by Graeme Cross for the LUV March 1999 talk

#include <stdio.h>

#define BUFSIZE 1024

main(int argc, char *argv[]) {

  char fileName[BUFSIZE];
  char password[BUFSIZE];
  char buf[BUFSIZE];
  FILE *f;
  if (argc != 3) {
    fprintf(stderr, "\nCIPHER syntax: cipher file password\n\n");
    /* we should be exiting here */

  /* what happens if argv[1] or argv[2] is greater than BUFSIZE? */
  strcpy(fileName, argv[1]);
  strcpy(password, argv[2]);

  if ((f = fopen(fileName, "r")) == NULL) {
    fprintf(stderr, "Error: could not open %s for reading!\n\n", fileName);

  while (!feof(f)) {
    /* does this correctly handle EOF cases? */
    if (fgets(buf, BUFSIZE, f) == EOF) {

    /* we would insert the encryption process here */

Prev - Index