public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "msebor at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/65113] New: string::copy violates traits requirements
Date: Thu, 19 Feb 2015 03:07:00 -0000	[thread overview]
Message-ID: <bug-65113-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65113

            Bug ID: 65113
           Summary: string::copy violates traits requirements
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gmail dot com

libstdc++ string goes to quite a bit of trouble to avoid self-modifying calls
that could potentially corrupt the controlled sequence but there is at least
one case that has escaped this treatment.  The test case below shows that
basic_string::copy(char*, size_t) calls char_traits::copy with arguments that
violate the preconditions on the function.  That function in turn calls memcpy
with arguments that violate its own constraints.

I suspect that this use case may not have been intended when string was
designed.  It might be perhaps be worth checking with the LWG to see if
basic_string::copy should require the argument to be distinct from the object
on which it's called.

$ cat t.cpp && for dbg in -DNDEBUG ""; do g++ $dbg -Wall t.cpp && ./a.out &&
valgrind ./a.out; done
#include <assert.h>
#include <string>

template <class CharT>
struct Traits: std::char_traits<CharT> {
    static char* copy (CharT *d, const CharT *s, size_t n) {
        assert (d < s || s + n < d);
        return std::char_traits<CharT>::copy (d, s, n);
    }
};

template <class CharT, class Traits>
void f (const CharT *a, size_t len)
{
    typedef std::basic_string<CharT, Traits> String;
    String str (a, len);
    CharT* const s = &str [2];
    const typename String::size_type n = str.size () - 2;

    str.copy (s, n, 0);
}

int main ()
{
    const char S[] = "0123456789";

    f<char, Traits<char> >(S, sizeof S - 1);
}

==14329== Memcheck, a memory error detector
==14329== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==14329== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==14329== Command: ./a.out
==14329== 
==14329== Source and destination overlap in memcpy(0x5a2005a, 0x5a20058, 8)
==14329==    at 0x4C2E13D: memcpy@@GLIBC_2.14 (in
/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==14329==    by 0x400DBD: std::char_traits<char>::copy(char*, char const*,
unsigned long) (in /home/msebor/tmp/a.out)
==14329==    by 0x401407: Traits<char>::copy(char*, char const*, unsigned long)
(in /home/msebor/tmp/a.out)
==14329==    by 0x4012A6: std::basic_string<char, Traits<char>,
std::allocator<char> >::_M_copy(char*, char const*, unsigned long) (in
/home/msebor/tmp/a.out)
==14329==    by 0x40105B: std::basic_string<char, Traits<char>,
std::allocator<char> >::copy(char*, unsigned long, unsigned long) const (in
/home/msebor/tmp/a.out)
==14329==    by 0x400E54: void f<char, Traits<char> >(char const*, unsigned
long) (in /home/msebor/tmp/a.out)
==14329==    by 0x400D3D: main (in /home/msebor/tmp/a.out)
==14329== 
==14329== 
==14329== HEAP SUMMARY:
==14329==     in use at exit: 0 bytes in 0 blocks
==14329==   total heap usage: 1 allocs, 1 frees, 35 bytes allocated
==14329== 
==14329== All heap blocks were freed -- no leaks are possible
==14329== 
==14329== For counts of detected and suppressed errors, rerun with: -v
==14329== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
a.out: t.cpp:7: static char* Traits<CharT>::copy(CharT*, const CharT*, size_t)
[with CharT = char; size_t = long unsigned int]: Assertion `d < s || s + n < d'
failed.
Aborted (core dumped)


             reply	other threads:[~2015-02-19  3:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-19  3:07 msebor at gmail dot com [this message]
2015-02-19 11:50 ` [Bug libstdc++/65113] " redi at gcc dot gnu.org
2020-10-28 15:33 ` redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-65113-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).