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

via libc-commits libc-commits at lists.llvm.org
Sun Oct 6 13:36:44 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=?Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/110363 at github.com>


================
@@ -26,6 +27,26 @@ static int64_t computeRemainingYears(int64_t daysPerYears,
   return years;
 }
 
+volatile int lock = 0;
+
+void release_file(FILE *fp) {
+  lock = 0;
+  fclose(fp);
+}
+
+void acquire_file(FILE *fp, char *timezone) {
+  while (1) {
+    if (lock == 0) {
+      lock = 1;
+      break;
+    }
+  }
+
+  if (fgets(timezone, sizeof(timezone), fp) == NULL) {
+    release_file(fp);
+  }
+}
----------------
graphite-app[bot] wrote:

The `acquire_file` function uses `sizeof(timezone)`, which is incorrect as `timezone` is a pointer. This could lead to a buffer overflow. Consider passing the size of the timezone buffer as a parameter to ensure safe memory access. For example:

```c
void acquire_file(FILE *fp, char *timezone, size_t timezone_size) {
    // ...
    if (fgets(timezone, timezone_size, fp) == NULL) {
        // ...
    }
}
```

This change would improve the function's safety and make its interface more explicit about the expected buffer size.

*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