Re: alias.c (potential bug found)

From: Katzlberger Thomas (cat@garfield.Vanderbilt.Edu)
Date: 09/18/95


Hi all!

Did anybody see the memory leak in the original code ?
It remided me on what I was discussing with Scatter.

Here is the leak free, more secure version I just
finished:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "structs.h"
#include "utils.h"
#include "interpreter.h"

void
write_aliases(Character * ch)
{
    FILE     *file;
    char      fn[127], *buf;
    struct alias *temp;
    int       length;

    get_filename(GET_NAME(ch), fn, ALIAS_FILE);
    unlink(fn);
    if (!GET_ALIASES(ch))
        return;

    file = fopen(fn, "wt");
    if (file == 0)
    {
        errlog("SYSERR: Can't create alias file!");
    }

    temp = GET_ALIASES(ch);

    while (temp)
    {
        length = strlen(temp->alias);
        length = MIN(length, 120);
        *(temp->alias + length) = 0;
        fprintf(file, "%d\n", length);
        fprintf(file, "%s\n", temp->alias);

        length = strlen(temp->replacement);
        length = MIN(length, 120);
        *(temp->replacement + length) = 0;

        buf = temp->replacement;
        while (*buf == ' ')
            //skip leading blanks
        {
            length--;
            buf++;
        }

        fprintf(file, "%d\n", length);
        fprintf(file, "%s\n", buf);
        fprintf(file, "%d\n", temp->type);
        temp = temp->next;
    }

    fclose(file);
}

void
read_aliases(Character * ch)
{
    FILE     *file;
    char      fn[127];
    struct alias *t2;
    int       length = 0;
    char      temp_buf[127], buf[127];

    get_filename(GET_NAME(ch), fn, ALIAS_FILE);

    file = fopen(fn, "r");

    if (!file)
        return;

    CREATE(GET_ALIASES(ch),struct alias,1);
    t2 = GET_ALIASES(ch);

    do
    {
        if (fscanf(file, "%d\n", &length) != 1)
        {
            fclose(file);
            return;
            //faulty file ! !
        }

  .............

No guarantee or warranty implied, Cat.
--
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/_/                                                            _/_/
_/_/  Thomas Katzlberger                                        _/_/
_/_/  katzlbt@vuse.vanderbilt.edu                               _/_/
_/_/  @aBlackNeXT.called.garfield                               _/_/
_/_/  http://www.vuse.vanderbilt.edu/~katzlbt/                  _/_/
_/_/                                                            _/_/
_/_/  "You can tune a file system, but you can't tune a fish."  _/_/
_/_/                                UNIX man page for tunefs.   _/_/
_/_/                                                            _/_/
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/



This archive was generated by hypermail 2b30 : 12/07/00 PST