From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 70605 invoked by alias); 6 Aug 2015 17:59:38 -0000 Mailing-List: contact gdb-prs-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-prs-owner@sourceware.org Received: (qmail 70579 invoked by uid 48); 6 Aug 2015 17:59:38 -0000 From: "dje at google dot com" To: gdb-prs@sourceware.org Subject: [Bug python/18779] New: collection of issues handling C strings in python Date: Thu, 06 Aug 2015 17:59:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gdb X-Bugzilla-Component: python X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: dje at google dot com X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at sourceware dot org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://sourceware.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2015-q3/txt/msg00157.txt.bz2 https://sourceware.org/bugzilla/show_bug.cgi?id=18779 Bug ID: 18779 Summary: collection of issues handling C strings in python Product: gdb Version: unknown Status: NEW Severity: normal Priority: P2 Component: python Assignee: unassigned at sourceware dot org Reporter: dje at google dot com Target Milestone: --- Wanting to extract a nul-terminated string from memory given just an ELF symbol I ran into a few warts/bugs. This bug is to record what I find, though I may not list them all in this initial report (I *could* file a separate report per bug, but I'm not sure how many there will be and since they're all related I wanted to keep them together). Given: const char string[] = "hello"; bug #1: (gdb) py print gdb.parse_and_eval("&string[0]").string(length=7,encoding="ASCII") Traceback (most recent call last): File "", line 1, in gdb.MemoryError: Cannot access memory at address 0x40075c Error while executing Python code. One can certainly argue that's an error, but not a memory access error. bug #2: (gdb) py print gdb.parse_and_eval("&string[0]").lazy_string() Question: what should str(lazy_string_object) be? If one wants the above there is "repr". The only useful thing to me seems to be the value returned as a python string. IOW I expected to see "hello". feature request #1: IWBN if one could specify that the string is nul-terminated, which we can by specifying a length of -1, but also specify a maximum length (for those wanting more robustness). maybe bug #3: I don't have a string opinion on whether this works or not, in c or c++, just filing this for reference sake since it seems odd: (gdb) py print gdb.parse_and_eval("&string").string() Traceback (most recent call last): File "", line 1, in gdb.error: Trying to read string with inappropriate type `const char (*)[6]'. Error while executing Python code. maybe bug #4: If we know the array's length, how come the lazy string length is always -1? (gdb) pt &string type = const char (*)[6] (gdb) py print gdb.parse_and_eval("&string[0]").lazy_string().length -1 (gdb) py print gdb.parse_and_eval("&string").lazy_string().length -1 (gdb) py print gdb.parse_and_eval("string").lazy_string().length -1 -- You are receiving this mail because: You are on the CC list for the bug.