Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #101738

Re: strtok() and the mistery of the same address strings with different lengths...

Newsgroups comp.lang.c
Date 2017-01-14 04:16 -0800
References <94292e94-bcb9-40ea-8b8a-2a8563eeb33e@googlegroups.com> <6e096b3f-1e35-49ae-8887-637f94fc6d51@googlegroups.com>
Message-ID <0151fc5f-a2eb-4ff3-b520-a6a0e424b6bc@googlegroups.com> (permalink)
Subject Re: strtok() and the mistery of the same address strings with different lengths...
From "Fernando D. Bozzo" <fdbozzo@gmail.com>

Show all headers | View raw


El sábado, 14 de enero de 2017, 12:19:04 (UTC+1), Fernando D. Bozzo  escribió:
> Let's say I create a malloc'ed copy of the original string with strdup(), then use strtok() for parsing it:
> 
> 1) Is this thread safe, given that I give it a new memory segment?
> 
> 2) Once I finish using it and the duplicated string is sub-segmented, which end pointing to the first segment: does free() know how to free the entire memory segment with all their sub-segments? I don't want to create a memory leak.
> 
> Thanks!


I answer myself:

2) NO, doing this totally corrupts the memory segment :(

This is what happen when I try it:

fernando@fer-desktop:~/DESA/C-Lang/Tests$ valgrind ./parse-0 
==25869== Memcheck, a memory error detector
==25869== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==25869== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==25869== Command: ./parse-0
==25869== 
==25869== Invalid write of size 1
==25869==    at 0x4008F1: strduplicate (in /home/fernando/DESA/C-Lang/Tests/parse-0)
==25869==    by 0x4006F6: main (in /home/fernando/DESA/C-Lang/Tests/parse-0)
==25869==  Address 0x5203098 is 24 bytes before an unallocated block of size 4,194,096 in arena "client"
==25869== 

valgrind: m_mallocfree.c:303 (get_bszB_as_is): Assertion 'bszB_lo == bszB_hi' failed.
valgrind: Heap block lo/hi size mismatch: lo = 4194272, hi = 0.
This is probably caused by your program erroneously writing past the
end of a heap block and corrupting heap metadata.  If you fix any
invalid writes reported by Memcheck, this assertion failure will
probably go away.  Please try that before reporting this as a bug.


host stacktrace:
==25869==    at 0x38083F48: show_sched_status_wrk (m_libcassert.c:343)
==25869==    by 0x38084064: report_and_quit (m_libcassert.c:415)
==25869==    by 0x380841F1: vgPlain_assert_fail (m_libcassert.c:481)
==25869==    by 0x380903A1: get_bszB_as_is (m_mallocfree.c:301)
==25869==    by 0x380903A1: get_bszB (m_mallocfree.c:311)
==25869==    by 0x380903A1: get_next_b (m_mallocfree.c:429)
==25869==    by 0x380903A1: unlinkBlock (m_mallocfree.c:1629)
==25869==    by 0x380920F5: vgPlain_arena_malloc (m_mallocfree.c:1810)
==25869==    by 0x38050E84: vgMemCheck_new_block (mc_malloc_wrappers.c:350)
==25869==    by 0x38051056: vgMemCheck_malloc (mc_malloc_wrappers.c:385)
==25869==    by 0x380D4F7B: do_client_request (scheduler.c:1865)
==25869==    by 0x380D4F7B: vgPlain_scheduler (scheduler.c:1425)
==25869==    by 0x380E3946: thread_wrapper (syswrap-linux.c:102)
==25869==    by 0x380E3946: run_a_thread_NORETURN (syswrap-linux.c:155)

sched status:
  running_tid=1

Thread 1: status = VgTs_Runnable (lwpid 25869)
==25869==    at 0x4C2DB8F: malloc (vg_replace_malloc.c:299)
==25869==    by 0x4EA71D4: _IO_file_doallocate (filedoalloc.c:127)
==25869==    by 0x4EB5593: _IO_doallocbuf (genops.c:398)
==25869==    by 0x4EB48F7: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:820)
==25869==    by 0x4EB328C: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1331)
==25869==    by 0x4E87240: vfprintf (vfprintf.c:1320)
==25869==    by 0x4E8F898: printf (printf.c:33)
==25869==    by 0x40073A: main (in /home/fernando/DESA/C-Lang/Tests/parse-0)


Note: see also the FAQ in the source distribution.
It contains workarounds to several common problems.
In particular, if Valgrind aborted or crashed after
identifying problems in your program, there's a good chance
that fixing those problems will prevent Valgrind aborting or
crashing, especially if it happened in m_mallocfree.c.

If that doesn't help, please report this bug to: www.valgrind.org

In the bug report, send all the above text, the valgrind
version, and what OS and version you are using.  Thanks.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-13 16:16 -0800
  Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-13 16:30 -0800
  Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 00:44 +0000
    Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-13 17:43 -0800
    Re: strtok() and the mistery of the same address strings with different lengths... Manfred <noname@invalid.add> - 2017-01-14 16:52 +0100
      Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 19:38 +0000
  Re: strtok() and the mistery of the same address strings with different lengths... Keith Thompson <kst-u@mib.org> - 2017-01-13 16:49 -0800
    Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-13 17:39 -0800
      Re: strtok() and the mistery of the same address strings with different lengths... James Kuyper <jameskuyper@verizon.net> - 2017-01-13 22:00 -0500
        Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 03:11 -0800
          Re: strtok() and the mistery of the same address strings with different lengths... James Kuyper <jameskuyper@verizon.net> - 2017-01-14 10:50 -0500
            Re: strtok() and the mistery of the same address strings with different lengths... Keith Thompson <kst-u@mib.org> - 2017-01-14 14:27 -0800
              Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 15:21 -0800
              Re: strtok() and the mistery of the same address strings with different lengths... Les Cargill <lcargill99@comcast.com> - 2017-01-15 11:48 -0600
                Re: strtok() and the mistery of the same address strings with different lengths... Keith Thompson <kst-u@mib.org> - 2017-01-15 15:24 -0800
                Re: strtok() and the mistery of the same address strings with different lengths... Les Cargill <lcargill99@comcast.com> - 2017-01-15 22:11 -0600
      Re: strtok() and the mistery of the same address strings with different lengths... Manfred <noname@invalid.add> - 2017-01-14 16:48 +0100
  Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 03:18 -0800
    Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 04:16 -0800
      Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 15:14 +0000
        Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 09:58 -0800
          Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 19:51 +0000
            Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 15:18 -0800
        Re: strtok() and the mistery of the same address strings with different lengths... Keith Thompson <kst-u@mib.org> - 2017-01-14 14:17 -0800
    Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 15:09 +0000
      Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 11:09 -0800
        Re: strtok() and the mistery of the same address strings with different lengths... luser droog <luser.droog@gmail.com> - 2017-01-14 11:41 -0800
        Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 20:04 +0000
        Re: strtok() and the mistery of the same address strings with different lengths... James Kuyper <jameskuyper@verizon.net> - 2017-01-14 15:09 -0500
          Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 15:22 -0800
      Re: strtok() and the mistery of the same address strings with different lengths... Keith Thompson <kst-u@mib.org> - 2017-01-14 14:21 -0800

csiph-web