[cllfst]

[ Thread Index | Date Index | More lists.tuxfamily.org/cllfst Archives ]


selem
J'ai un exercice en programmation C et j'aime b1 d'aide pour le résoudre .
Jazakom Allah kola 5ayr :)
Voici l'exercice:
*)This C function performs its job most of the time, but has several defects (bugs, error handling, etc.) in specific situations.

Modify the code to make it as robust as possible, in order to make sure it will not crash or cause any problem in the application, even if the arguments provided to the function are incorrect.

/*
  double_whitespaces:
  Copy the "msg" buffer of size "size" into the "dest" buffer,
  while doubling any blank character found in the source buffer.
*/

int double_whitespaces(char *msg, int size, char *dest) {
  int ret;
  char *ptr;

  char *buf;

  buf = malloc(size);
    
  ptr = msg;
  while (ptr < msg + size) {
    if (*ptr == '%')     // % char is forbidden
      goto error;
    if (*ptr == ' ')  {
      *buf++ = ' ';
      *buf++ = ' ';
    } else {
      *buf++ = *ptr;

    }
    ptr++;
  }
  free(buf);
  strcpy(dest, buf);
  return 0;
error:
  return -1;
}



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/