)]}'
{
  "commit": "67ea24cb99efcd50d8acb6ce3e3ffbd8c97f0829",
  "tree": "31dcd343217cede2087c7fb1b794ef7da81d9bd2",
  "parents": [
    "f9e5d80cf75c4c549c392b6bb1bd33e103824657"
  ],
  "author": {
    "name": "Simon Marchi",
    "email": "simon.marchi@polymtl.ca",
    "time": "Tue Jul 13 11:31:42 2021 -0400"
  },
  "committer": {
    "name": "Pedro Alves",
    "email": "pedro@palves.net",
    "time": "Thu Jul 15 15:10:52 2021 +0100"
  },
  "message": "gdb: consider null terminator for length arguments of value_cstring calls\n\nThis patch started when I looked at this bit in cli/cli-cmds.c:\n\n    if (*(char **) cmd-\u003evar)\n      return value_cstring (*(char **) cmd-\u003evar, strlen (*(char **) cmd-\u003evar),\n\t\t\t    builtin_type (gdbarch)-\u003ebuiltin_char);\n    else\n      return value_cstring (\"\", 1,\n\t\t\t    builtin_type (gdbarch)-\u003ebuiltin_char);\n\nI found it odd that the first value_cstring call passes a length that\ndoes not consider the null terminator of the C string, but second does.\nI want to clarify the documentation of value_cstring and fix the one\nthat is wrong.\n\nDebugging a little bit, I found that the first call is the wrong one.\nDoing:\n\n  (gdb) set args foo\n  (gdb) print $_gdb_setting(\"args\")\n  $1 \u003d \"foo\"\n\ncreates a struct value of code TYPE_CODE_ARRAY of size 3, which doesn\u0027t\nhave a null terminator.  That does not create a valid C string.  It is\nhowever printed correctly by GDB, because the printing code makes sure\nnot to read past the value\u0027s length.\n\nA way to expose the bug is to print each element of the created string,\nincluding the null terminator.  Before:\n\n    (gdb) set args foo\n    (gdb) p $_gdb_setting(\"args\")[3]\n    no such vector element\n\nAfter:\n\n    (gdb) set args foo\n    (gdb) p $_gdb_setting(\"args\")[3]\n    $1 \u003d 0 \u0027\\000\u0027\n\nAnother perhaps more convincing way of showing the bug is if the string\nvalue is passed to an inferior function call;\n\n    (gdb) print an_inferior_function($_gdb_setting(\"args))\n\nThe space allocate for the string in the inferior will not take into\naccount a null terminator (with the string \"foo\", 3 bytes will be\nallocated).  If the inferior tries to read the string until the null\nterminator, it will read past the allocated buffer.  Compiling the\ninferior with AddressSanitizer makes that bad access obvious.\n\nI found a few calls to value_cstring that were wrong, I fixed them\nall, all exercised by the test.\n\nThe change in guile/scm-math.c maybe requires a bit of explanation.\nAccording to the doc of gdbscm_scm_to_string, if don\u0027t pass a length\npointer, we get back a null-terminated string.  If we pass a length\npointer, we get a non-null-terminated string, but we get the length\nseparately.  Trying to pass \"len + 1\" to value_cstring would lead to GDB\nreading past the allocated buffer, that is exactly of length `len`.  So\ninstead, don\u0027t pass a length pointer and use strlen on the result.\n\ngdb.base/settings.exp and gdb.python/py-mi.exp required changes in some\nexpected outputs, because the array type created by $_gdb_setting_str\nis now one element larger, to account for the null terminator.  I don\u0027t\nthink this change in user-visible behavior is a problem.\n\nChange-Id: If8dd13cce55c70521e34e7c360139064b4e87496\n",
  "tree_diff": [
    {
      "type": "modify",
      "old_id": "56ae12a0c19d6c75a40b605de92380c933035908",
      "old_mode": 33188,
      "old_path": "gdb/cli/cli-cmds.c",
      "new_id": "f6fc5ab854f457cac0500a97c47038263e892dee",
      "new_mode": 33188,
      "new_path": "gdb/cli/cli-cmds.c"
    },
    {
      "type": "modify",
      "old_id": "d9fd67181966346f1dbead57ed3df6e89c0388b8",
      "old_mode": 33188,
      "old_path": "gdb/guile/scm-math.c",
      "new_id": "8b7fe9b69ecd47f342080871e9164a1de9c8add4",
      "new_mode": 33188,
      "new_path": "gdb/guile/scm-math.c"
    },
    {
      "type": "modify",
      "old_id": "8df8a15f8d6b2697118cadfd45d7aff55812dc0e",
      "old_mode": 33188,
      "old_path": "gdb/python/py-value.c",
      "new_id": "bc0f88b7a16632ae57e07c94bd46a18365910c73",
      "new_mode": 33188,
      "new_path": "gdb/python/py-value.c"
    },
    {
      "type": "add",
      "old_id": "0000000000000000000000000000000000000000",
      "old_mode": 0,
      "old_path": "/dev/null",
      "new_id": "b9c7c847cbf5b8c826f4fa41de788018e883dc43",
      "new_mode": 33188,
      "new_path": "gdb/testsuite/gdb.base/internal-string-values.c"
    },
    {
      "type": "add",
      "old_id": "0000000000000000000000000000000000000000",
      "old_mode": 0,
      "old_path": "/dev/null",
      "new_id": "0748ab0984c6a2941e5cd5b44a8b715559f34005",
      "new_mode": 33188,
      "new_path": "gdb/testsuite/gdb.base/internal-string-values.exp"
    },
    {
      "type": "modify",
      "old_id": "64257aaae5a7082e2c3ad505435fca090a6c8eba",
      "old_mode": 33188,
      "old_path": "gdb/testsuite/gdb.base/settings.exp",
      "new_id": "0a594685d6b13c2f1497b891b7e1819bbe477645",
      "new_mode": 33188,
      "new_path": "gdb/testsuite/gdb.base/settings.exp"
    },
    {
      "type": "modify",
      "old_id": "6f0c1c65e62ae4ed52ad0f3802c6c13a8258e466",
      "old_mode": 33188,
      "old_path": "gdb/testsuite/gdb.python/py-mi.exp",
      "new_id": "b81ee9acd747f0d459933da799b804399f8bea57",
      "new_mode": 33188,
      "new_path": "gdb/testsuite/gdb.python/py-mi.exp"
    },
    {
      "type": "modify",
      "old_id": "bd547923496f4b39a3651b47679771eaf3feb4fc",
      "old_mode": 33188,
      "old_path": "gdb/valops.c",
      "new_id": "f67612e87b082c1ada4f3cc3d25fa6bd30476ac9",
      "new_mode": 33188,
      "new_path": "gdb/valops.c"
    },
    {
      "type": "modify",
      "old_id": "6a07495d32bcbb6035f8c1200f39237b31a45e9e",
      "old_mode": 33188,
      "old_path": "gdb/value.c",
      "new_id": "db34d2e5762f5520207fda19750233cfea9d1392",
      "new_mode": 33188,
      "new_path": "gdb/value.c"
    },
    {
      "type": "modify",
      "old_id": "379cddafbe7fa8064110d7595cf296ca0b3127e1",
      "old_mode": 33188,
      "old_path": "gdb/value.h",
      "new_id": "24392437ed0bd7f43d21ec7881e5997308ecab21",
      "new_mode": 33188,
      "new_path": "gdb/value.h"
    }
  ]
}
