]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Do not modify a character string that could be a readonly
authorFlorian Krohm <florian@eich-krohm.de>
Sat, 6 Sep 2014 20:40:28 +0000 (20:40 +0000)
committerFlorian Krohm <florian@eich-krohm.de>
Sat, 6 Sep 2014 20:40:28 +0000 (20:40 +0000)
string literal.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@14485

coregrind/m_options.c

index bc399df948b09b41bcab134ad5c08fffdedf44d4..b1885540456d72baba4c84eeb674733feb69eab0 100644 (file)
@@ -204,32 +204,24 @@ HChar* VG_(expand_file_name)(const HChar* option_name, const HChar* format)
             i++;
             if ('{' == format[i]) {
                // Get the env var name, print its contents.
-               const HChar* qualname;
-               HChar* qual;
-               i++;
-               qualname = &format[i];
+               HChar *qual;
+               Int begin_qualname = ++i;
                while (True) {
                   if (0 == format[i]) {
                      VG_(fmsg)("%s: malformed %%q specifier\n", option_name);
                      goto bad;
                   } else if ('}' == format[i]) {
-                     // Temporarily replace the '}' with NUL to extract var
-                     // name.
-                     // FIXME: this is not safe as FORMAT is sometimes a
-                     // string literal which may reside in read-only memory
-                    ((HChar *)format)[i] = 0;
+                     Int qualname_len = i - begin_qualname;
+                     HChar qualname[qualname_len + 1];
+                     VG_(strncpy)(qualname, format + begin_qualname,
+                                  qualname_len);
+                     qualname[qualname_len] = '\0';
                      qual = VG_(getenv)(qualname);
                      if (NULL == qual) {
                         VG_(fmsg)("%s: environment variable %s is not set\n",
                                   option_name, qualname);
-                     // FIXME: this is not safe as FORMAT is sometimes a
-                     // string literal which may reside in read-only memory
-                        ((HChar *)format)[i] = '}';  // Put the '}' back.
                         goto bad;
                      }
-                     // FIXME: this is not safe as FORMAT is sometimes a
-                     // string literal which may reside in read-only memory
-                     ((HChar *)format)[i] = '}';     // Put the '}' back.
                      i++;
                      break;
                   }