Skip to content
Permalink
Browse files

bufchk_len:add new function and create a test for it

  • Loading branch information...
andy5995 committed Jun 24, 2019
1 parent bc221d1 commit edaf560929e8589bac8874b93ae3520962ffab39
Showing with 69 additions and 9 deletions.
  1. +11 −0 src/messages_rmw.c
  2. +4 −1 src/messages_rmw.h
  3. +20 −5 src/strings_rmw.c
  4. +3 −0 src/strings_rmw.h
  5. +9 −1 src/trashinfo_rmw.c
  6. +3 −1 test/Makefile.am
  7. +18 −0 test/buffer_overrun.c
  8. +1 −1 test/strings.c
@@ -202,3 +202,14 @@ msg_err_fatal_fprintf (const char *func)
fprintf (stderr, "fprintf returned an error in %s.\n", func);
exit (EXIT_FAILURE);
}

void
msg_err_buffer_overrun (const char *func, const int line)
{
fprintf (stderr, "func = %s, line = %d\n", func, line);
/* TRANSLATORS: "buffer" in the following instances refers to the amount
* of memory allocated for a string */
fputs (_("buffer overrun (segmentation fault) prevented.\n"), stderr);
fputs (_("If you think this may be a bug, please report it to the rmw developers.\n"), stderr);
}

@@ -5,7 +5,7 @@
*
* This file is part of rmw<https://remove-to-waste.info/>
*
* Copyright (C) 2012-2018 Andy Alt (andy400-dev@yahoo.com)
* Copyright (C) 2012-2019 Andy Alt (andy400-dev@yahoo.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -59,3 +59,6 @@ void
msg_err_rename (const char *src_file, const char *dest_file, const char *func, const int line);

void msg_err_fatal_fprintf (const char *func);

void
msg_err_buffer_overrun (const char *func, const int line);
@@ -86,11 +86,10 @@ bufchk (const char *str, ushort boundary)
#endif

print_msg_error ();
/* TRANSLATORS: "buffer" in the following instances refers to the amount
* of memory allocated for a string */
printf (_("buffer overrun (segmentation fault) prevented.\n"));
printf (_
("If you think this may be a bug, please report it to the rmw developers.\n"));
/*
* For now, using arbitrary arguments instead of changing every call to bufchk
*/
msg_err_buffer_overrun ("unknown", 0);

/*
* This will add a null terminator within the boundary specified by
@@ -121,6 +120,22 @@ bufchk (const char *str, ushort boundary)
exit (EXIT_BUF_ERR);
}

void
bufchk_len (const int len, const int boundary, const char *func, const int line)
{
if (len <= boundary)
return;

#ifdef TEST_LIB
errno = 1;
return;
#endif

print_msg_error ();
msg_err_buffer_overrun (func, line);
exit (EXIT_BUF_ERR);
}

/*!
* Get the combined length of multiple strings.
*
@@ -31,6 +31,9 @@
void
bufchk (const char *str, unsigned short boundary);

void
bufchk_len (const int len, const int boundary, const char *func, const int line);

int
multi_strlen (int argc, ...);

@@ -122,10 +122,18 @@ printf ("dest = %s in %s\n", dest, __func__);
int
create_trashinfo (rmw_target *file, st_waste *waste_curr)
{
/*
* there already should have been buffer checking on these 2 when they were
* initialized
*/
int req_len = multi_strlen (2, waste_curr->info, file->base_name) + 1;

/*
* Make sure there's enough room in file_info_dest
*/
bufchk_len (req_len, MP, __func__, __LINE__);
char final_info_dest[MP];
snprintf (final_info_dest, req_len, "%s%s", waste_curr->info, file->base_name);
bufchk (final_info_dest, MP);

#ifdef DEBUG
DEBUG_PREFIX
@@ -3,11 +3,13 @@ CFLAGS = -I$(top_srcdir)/src
check_PROGRAMS = \
strings \
config_test \
multi_strlen
multi_strlen \
buffer_overrun

strings_SOURCES = strings.c
config_test_SOURCES = config_test.c
multi_strlen_SOURCES = multi_strlen.c
buffer_overrun_SOURCES = buffer_overrun.c

test_scripts = \
basic.sh \
@@ -0,0 +1,18 @@
#include "rmw.h"
#include "strings_rmw.h"
#include <assert.h>

int
main ()
{
int req_len = MP;
bufchk_len (req_len, MP, __func__, __LINE__);

req_len = MP - 1;
bufchk_len (req_len, MP, __func__, __LINE__);

req_len = MP + 1;
bufchk_len (req_len, MP, __func__, __LINE__);
assert (errno);
errno = 0;
}
@@ -19,7 +19,7 @@ main ()
chk_malloc (test, __func__, __LINE__);
test[0] = '\0';
trim_white_space (test);
assert (errno == 1);
assert (errno);
errno = 0;

strcpy (test, " \n\t\v\f\r ");

0 comments on commit edaf560

Please sign in to comment.
You can’t perform that action at this time.