-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc] add integer writing to printf
This patch adds %n to printf, as well as a compiler flag to disable it. This is due to it having serious security issues when misused. Reviewed By: lntue Differential Revision: https://reviews.llvm.org/D127517
- Loading branch information
1 parent
f239cdd
commit 88b8013
Showing
6 changed files
with
112 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//===-- Write integer Converter for printf ----------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H | ||
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H | ||
|
||
#include "src/__support/CPP/Limits.h" | ||
#include "src/stdio/printf_core/core_structs.h" | ||
#include "src/stdio/printf_core/writer.h" | ||
|
||
#include <inttypes.h> | ||
#include <stddef.h> | ||
|
||
namespace __llvm_libc { | ||
namespace printf_core { | ||
|
||
int inline convert_write_int(Writer *writer, const FormatSection &to_conv) { | ||
|
||
// This is an additional check added by LLVM-libc. The reason it returns -3 is | ||
// because printf uses negative return values for errors, and -1 and -2 are | ||
// already in use by the file_writer class for file errors. | ||
if (to_conv.conv_val_ptr == nullptr) | ||
return -3; | ||
|
||
int written = writer->get_chars_written(); | ||
|
||
switch (to_conv.length_modifier) { | ||
case LengthModifier::none: | ||
*reinterpret_cast<int *>(to_conv.conv_val_ptr) = written; | ||
break; | ||
case LengthModifier::l: | ||
*reinterpret_cast<long *>(to_conv.conv_val_ptr) = written; | ||
break; | ||
case LengthModifier::ll: | ||
case LengthModifier::L: | ||
*reinterpret_cast<long long *>(to_conv.conv_val_ptr) = written; | ||
break; | ||
case LengthModifier::h: | ||
*reinterpret_cast<short *>(to_conv.conv_val_ptr) = written; | ||
break; | ||
case LengthModifier::hh: | ||
*reinterpret_cast<signed char *>(to_conv.conv_val_ptr) = written; | ||
break; | ||
case LengthModifier::z: | ||
*reinterpret_cast<size_t *>(to_conv.conv_val_ptr) = written; | ||
break; | ||
case LengthModifier::t: | ||
*reinterpret_cast<ptrdiff_t *>(to_conv.conv_val_ptr) = written; | ||
break; | ||
case LengthModifier::j: | ||
*reinterpret_cast<uintmax_t *>(to_conv.conv_val_ptr) = written; | ||
break; | ||
} | ||
return 0; | ||
} | ||
|
||
} // namespace printf_core | ||
} // namespace __llvm_libc | ||
|
||
#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_WRITE_INT_CONVERTER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters