LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/asn1 - gen_free.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 82 122 67.2 %
Date: 2024-06-13 04:01:37 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
       3             :  * (Royal Institute of Technology, Stockholm, Sweden).
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions
       8             :  * are met:
       9             :  *
      10             :  * 1. Redistributions of source code must retain the above copyright
      11             :  *    notice, this list of conditions and the following disclaimer.
      12             :  *
      13             :  * 2. Redistributions in binary form must reproduce the above copyright
      14             :  *    notice, this list of conditions and the following disclaimer in the
      15             :  *    documentation and/or other materials provided with the distribution.
      16             :  *
      17             :  * 3. Neither the name of the Institute nor the names of its contributors
      18             :  *    may be used to endorse or promote products derived from this software
      19             :  *    without specific prior written permission.
      20             :  *
      21             :  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
      22             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      23             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      24             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
      25             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      26             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      27             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      28             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      29             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      30             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      31             :  * SUCH DAMAGE.
      32             :  */
      33             : 
      34             : #include "gen_locl.h"
      35             : 
      36             : RCSID("$Id$");
      37             : 
      38             : static void
      39         476 : free_primitive (const char *typename, const char *name)
      40             : {
      41         476 :     fprintf (codefile, "der_free_%s(%s);\n", typename, name);
      42         476 : }
      43             : 
      44             : static void
      45        5516 : free_type (const char *name, const Type *t, int preserve)
      46             : {
      47        5516 :     switch (t->type) {
      48        1134 :     case TType:
      49             : #if 0
      50             :         free_type (name, t->symbol->type, preserve);
      51             : #endif
      52        1134 :         fprintf (codefile, "free_%s(%s);\n", t->symbol->gen_name, name);
      53        1134 :         break;
      54         308 :     case TInteger:
      55         308 :         if (t->range == NULL && t->members == NULL) {
      56          56 :             free_primitive ("heim_integer", name);
      57          56 :             break;
      58             :         }
      59             :         /* fallthrough; */
      60             :     case TBoolean:
      61             :     case TEnumerated :
      62             :     case TNull:
      63             :     case TGeneralizedTime:
      64             :     case TUTCTime:
      65             :         /*
      66             :          * This doesn't do much, but it leaves zeros where garbage might
      67             :          * otherwise have been found.  Gets us closer to having the equivalent
      68             :          * of a memset()-to-zero data structure after calling the free
      69             :          * functions.
      70             :          */
      71         294 :         fprintf(codefile, "*%s = 0;\n", name);
      72         294 :         break;
      73         140 :     case TBitString:
      74         140 :         if (HEIM_TAILQ_EMPTY(t->members))
      75          98 :             free_primitive("bit_string", name);
      76         140 :         break;
      77         196 :     case TOctetString:
      78         196 :         free_primitive ("octet_string", name);
      79         196 :         break;
      80         532 :     case TChoice:
      81             :     case TSet:
      82             :     case TSequence: {
      83         532 :         Member *m, *have_ellipsis = NULL;
      84             : 
      85         532 :         if (t->members == NULL)
      86           0 :             break;
      87             : 
      88         532 :         if ((t->type == TSequence || t->type == TChoice) && preserve)
      89           0 :             fprintf(codefile, "der_free_octet_string(&data->_save);\n");
      90             : 
      91         532 :         if(t->type == TChoice)
      92         126 :             fprintf(codefile, "switch((%s)->element) {\n", name);
      93             : 
      94        2422 :         HEIM_TAILQ_FOREACH(m, t->members, members) {
      95             :             char *s;
      96             : 
      97        1890 :             if (m->ellipsis){
      98          84 :                 have_ellipsis = m;
      99          84 :                 continue;
     100             :             }
     101             : 
     102        1806 :             if(t->type == TChoice)
     103         462 :                 fprintf(codefile, "case %s:\n", m->label);
     104        5418 :             if (asprintf (&s, "%s(%s)->%s%s",
     105        1806 :                           m->optional ? "" : "&", name,
     106        3612 :                           t->type == TChoice ? "u." : "", m->gen_name) < 0 || s == NULL)
     107           0 :                 errx(1, "malloc");
     108        1806 :             if(m->optional)
     109         700 :                 fprintf(codefile, "if(%s) {\n", s);
     110        1806 :             free_type (s, m->type, FALSE);
     111        1806 :             if(m->optional)
     112         700 :                 fprintf(codefile,
     113             :                         "free(%s);\n"
     114             :                         "%s = NULL;\n"
     115             :                         "}\n",s, s);
     116        1806 :             free (s);
     117        1806 :             if(t->type == TChoice)
     118         462 :                 fprintf(codefile, "break;\n");
     119             :         }
     120             : 
     121         532 :         if(t->type == TChoice) {
     122         126 :             if (have_ellipsis)
     123          14 :                 fprintf(codefile,
     124             :                         "case %s:\n"
     125             :                         "der_free_octet_string(&(%s)->u.%s);\n"
     126             :                         "break;",
     127             :                         have_ellipsis->label,
     128             :                         name, have_ellipsis->gen_name);
     129         126 :             fprintf(codefile, "}\n");
     130             :         }
     131         532 :         break;
     132             :     }
     133         224 :     case TSetOf:
     134             :     case TSequenceOf: {
     135             :         char *n;
     136             : 
     137         224 :         fprintf (codefile, "if ((%s)->val)\nwhile((%s)->len){\n", name, name);
     138         224 :         if (asprintf (&n, "&(%s)->val[(%s)->len-1]", name, name) < 0 || n == NULL)
     139           0 :             errx(1, "malloc");
     140         224 :         free_type(n, t->subtype, FALSE);
     141         224 :         fprintf(codefile,
     142             :                 "(%s)->len--;\n"
     143             :                 "} else (%s)->len = 0;\n",
     144             :                 name, name);
     145         224 :         fprintf(codefile,
     146             :                 "free((%s)->val);\n"
     147             :                 "(%s)->val = NULL;\n", name, name);
     148         224 :         free(n);
     149         224 :         break;
     150             :     }
     151          14 :     case TGeneralString:
     152          14 :         free_primitive ("general_string", name);
     153          14 :         break;
     154           0 :     case TTeletexString:
     155           0 :         free_primitive ("general_string", name);
     156           0 :         break;
     157          56 :     case TUTF8String:
     158          56 :         free_primitive ("utf8string", name);
     159          56 :         break;
     160           0 :     case TPrintableString:
     161           0 :         free_primitive ("printable_string", name);
     162           0 :         break;
     163           0 :     case TIA5String:
     164           0 :         free_primitive ("ia5_string", name);
     165           0 :         break;
     166           0 :     case TBMPString:
     167           0 :         free_primitive ("bmp_string", name);
     168           0 :         break;
     169           0 :     case TUniversalString:
     170           0 :         free_primitive ("universal_string", name);
     171           0 :         break;
     172           0 :     case TVisibleString:
     173           0 :         free_primitive ("visible_string", name);
     174           0 :         break;
     175        2814 :     case TTag:
     176        2814 :         free_type (name, t->subtype, preserve);
     177        2814 :         break;
     178          56 :     case TOID :
     179          56 :         free_primitive ("oid", name);
     180          56 :         break;
     181           0 :     default :
     182           0 :         abort ();
     183             :     }
     184        5516 : }
     185             : 
     186             : void
     187         672 : generate_type_free (const Symbol *s)
     188             : {
     189             :     struct decoration deco;
     190         672 :     ssize_t more_deco = -1;
     191         672 :     int preserve = preserve_type(s->name) ? TRUE : FALSE;
     192             : 
     193         672 :     fprintf (codefile, "void ASN1CALL\n"
     194             :              "free_%s(%s *data)\n"
     195             :              "{\n",
     196             :              s->gen_name, s->gen_name);
     197             : 
     198         672 :     free_type ("data", s->type, preserve);
     199        1358 :     while (decorate_type(s->gen_name, &deco, &more_deco)) {
     200          14 :         if (deco.ext && deco.free_function_name == NULL) {
     201             :             /* Decorated with field of external type but no free function */
     202          28 :             if (deco.ptr)
     203           0 :                 fprintf(codefile, "(data)->%s = 0;\n", deco.field_name);
     204             :             else
     205          14 :                 fprintf(codefile,
     206             :                         "memset(&(data)->%s, 0, sizeof((data)->%s));\n",
     207             :                         deco.field_name, deco.field_name);
     208           0 :         } else if (deco.ext) {
     209             :             /* Decorated with field of external type w/ free function */
     210           0 :             if (deco.ptr) {
     211           0 :                 fprintf(codefile, "if ((data)->%s) {\n", deco.field_name);
     212           0 :                 fprintf(codefile, "%s((data)->%s);\n",
     213             :                         deco.free_function_name, deco.field_name);
     214           0 :                 fprintf(codefile, "(data)->%s = 0;\n", deco.field_name);
     215           0 :                 fprintf(codefile, "}\n");
     216             :             } else {
     217           0 :                 fprintf(codefile, "%s(&(data)->%s);\n",
     218             :                         deco.free_function_name, deco.field_name);
     219           0 :                 fprintf(codefile,
     220             :                         "memset(&(data)->%s, 0, sizeof((data)->%s));\n",
     221             :                         deco.field_name, deco.field_name);
     222             :             }
     223           0 :         } else if (deco.opt) {
     224             :             /* Decorated with optional field of ASN.1 type */
     225           0 :             fprintf(codefile, "if ((data)->%s) {\n", deco.field_name);
     226           0 :             fprintf(codefile, "free_%s((data)->%s);\n",
     227             :                     deco.field_type, deco.field_name);
     228           0 :             fprintf(codefile, "free((data)->%s);\n", deco.field_name);
     229           0 :             fprintf(codefile, "(data)->%s = NULL;\n", deco.field_name);
     230           0 :             fprintf(codefile, "}\n");
     231             :         } else {
     232             :             /* Decorated with required field of ASN.1 type */
     233           0 :             fprintf(codefile, "free_%s(&(data)->%s);\n",
     234             :                     deco.field_type, deco.field_name);
     235             :         }
     236          14 :         free(deco.field_type);
     237             :     }
     238         672 :     fprintf (codefile, "}\n\n");
     239         672 : }
     240             : 

Generated by: LCOV version 1.13