[libc-commits] [libc] [libc] implement localtime (PR #110363)

via libc-commits libc-commits at lists.llvm.org
Tue Oct 8 06:05:17 PDT 2024


=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?=,
=?utf-8?b?0JfQuNGI0LDQvSDQnNC40YDQtw=?Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/110363 at github.com>


================
@@ -129,6 +154,27 @@ int64_t update_from_seconds(int64_t total_seconds, struct tm *tm) {
   if (years > INT_MAX || years < INT_MIN)
     return time_utils::out_of_range();
 
+  char *timezone = (char *)malloc(sizeof(char) * TimeConstants::TIMEZONE_SIZE);
+  timezone = getenv("TZ");
+  FILE *fp = NULL;
+  if (timezone == NULL) {
+    timezone =
+        (char *)realloc(timezone, sizeof(char) * TimeConstants::TIMEZONE_SIZE);
+    fp = fopen("/etc/timezone", "rb");
+    if (fp == NULL) {
+      return time_utils::out_of_range();
+    }
+
+    acquire_file(fp, timezone, TimeConstants::TIMEZONE_SIZE);
+  }
+
+  if (fp != NULL && file_usage == 0) {
+    release_file(fp, timezone);
+    return time_utils::out_of_range();
----------------
graphite-app[bot] wrote:

The current implementation may lead to a memory leak. The `timezone` variable is allocated with `malloc()`, but there's no corresponding `free()` call if `getenv()` succeeds. Consider one of the following approaches to address this:

1. Free the memory after it's no longer needed:
   ```c
   char *timezone = (char *)malloc(sizeof(char) * TimeConstants::TIMEZONE_SIZE);
   char *env_tz = getenv("TZ");
   if (env_tz) {
     strncpy(timezone, env_tz, TimeConstants::TIMEZONE_SIZE);
     timezone[TimeConstants::TIMEZONE_SIZE - 1] = '\0';
   } else {
     // ... existing code to read from file ...
   }
   
   // Use timezone...
   
   free(timezone);
   ```

2. Use a stack-allocated buffer instead of dynamic allocation:
   ```c
   char timezone[TimeConstants::TIMEZONE_SIZE];
   char *env_tz = getenv("TZ");
   if (env_tz) {
     strncpy(timezone, env_tz, sizeof(timezone));
     timezone[sizeof(timezone) - 1] = '\0';
   } else {
     // ... existing code to read from file ...
   }
   ```

These approaches would help ensure proper memory management and prevent potential leaks.

*Spotted by [Graphite Reviewer](https://app.graphite.dev/graphite-reviewer/?org=llvm&ref=ai-review-comment)*<i class='graphite__hidden'><br /><br />Is this helpful? React 👍 or 👎 to let us know.</i>

https://github.com/llvm/llvm-project/pull/110363


More information about the libc-commits mailing list