help-libtasn1
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Copies of argv elements in asn1Decoding


From: Pascal Cuoq
Subject: Copies of argv elements in asn1Decoding
Date: Sun, 22 May 2016 19:55:51 +0000

Hello,

this is how part of src/asn1Decoding.c currently looks 
(https://github.com/pascal-cuoq/libtasn1-fork/blob/67197e259aa8be1ce2ae4b048e01a9521572fc48/src/asn1Decoding.c#L145-L238
 ):


  inputFileAsnName = (char *) malloc (strlen (argv[optind]) + 1);
  strcpy (inputFileAsnName, argv[optind]);

  inputFileDerName = (char *) malloc (strlen (argv[optind + 1]) + 1);
  strcpy (inputFileDerName, argv[optind + 1]);

  typeName = (char *) malloc (strlen (argv[optind + 2]) + 1);
  strcpy (typeName, argv[optind + 2]);
 ...
  if (asn1_result != ASN1_SUCCESS)
    {
      free (inputFileAsnName);
      free (inputFileDerName);
      free (typeName);
      exit (1);
    }
...


I am not sure why it is necessary to copy argv[optind], argv[optind + 1] and 
argv[optind + 2] to allocated blocks. It seems to me that “ inputFileAsnName = 
argv[optind]; ...” would work just as well.

Regardless, if these strings are copied, the copies can fail.

The commit at 
https://github.com/pascal-cuoq/libtasn1-fork/commit/073b4d1c4c9247490ac7e9ac9157f20d4eb1e09f
 adds an allocation check covering these three copies:

Note 1: one may consider strdup instead of strlen + malloc + strcpy, but strdup 
is not in standard C despite being in SVr4, 4.3BSD, and POSIX.1-2001. 
Presumably this is why it is not used. I still think that inputFileAsnName = 
argv[optind]; … (and getting rid of all the free calls for these variables) 
would be simpler.

Note 2: these malloc failures are unlikely but checking the result of malloc 
here allows to check that the functions deeper inside the library do not  
behave wrongly when malloc returns NULL.

Pascal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]