format-string

Is it possible to use format strings to align NSStrings like numbers can be?

旧城冷巷雨未停 提交于 2019-12-05 00:00:29
I'm using NSLog() to print some tabular data consisting of an NSString and an associated integer . Assume I know the length of the longest word. Is there a way using format strings to get this kind of column alignment: word:tree rank:5 word:frog rank:3 word:house rank:2 word:peppercorn rank:2 word:sword rank:2 word:antlion rank:1 The reason I'm asking about formatting strings is I'm hoping for a lightweight way to format my ghetto debugging output. Here is what I tried: NSString *word = @"tree"; NSUInteger rank = 4; NSString *str = [NSString stringWithFormat:@"word:%-20@ rank:%u", word, rank];

Print arguments of a function using Clang AST

时光毁灭记忆、已成空白 提交于 2019-12-04 11:23:09
问题 I want to get the arguments passed to a function. for example, if I have the call printf("%d%d", i, j); the output should be %d%d i j I am able to get to function calls using VisitCallExpr() in RecursiveASTVisitor. Also able to get the number of arguments and the argument types. But I don't know how to get the arguments. bool MyRecursiveASTVisitor::VisitCallExpr (clang::CallExpr *E) { for(int i=0, j=E->getNumArgs(); i<j; i++) { llvm::errs() << "argType: " << E->getArg(i)->getType()

How to check that two format strings are compatible?

三世轮回 提交于 2019-12-03 09:21:05
Examples: "Something %d" and "Something else %d" // Compatible "Something %d" and "Something else %f" // Not Compatible "Something %d" and "Something %d else %d" // Not Compatible "Something %d and %f" and "Something %2$f and %1$d" // Compatible I figured there should be some C function for this, but I'm not getting any relevant search results. I mean the compiler is checking that the format string and the arguments match, so the code for checking this is already written. The only question is how I can call it. I'm using Objective-C, so if there is an Objective-C specific solution that's fine

Does printf() allocate memory in C?

喜欢而已 提交于 2019-12-01 11:47:32
This simple method just creates an array of dynamic size n and initializes it with values 0 ... n-1. It contains a mistake, malloc() allocates just n instead of sizeof(int) * n bytes: int *make_array(size_t n) { int *result = malloc(n); for (int i = 0; i < n; ++i) { //printf("%d", i); result[i] = i; } return result; } int main() { int *result = make_array(8); for (int i = 0; i < 8; ++i) { printf("%d ", result[i]); } free(result); } When you check the output you will see that it will print some numbers as expected but the last ones are gibberish. However, once I inserted the printf() inside the

Format string attack in printf

倖福魔咒の 提交于 2019-12-01 08:44:05
#include <stdio.h> int main() { char s[200] int a=123; int b=&a; scanf("%50s",s); printf(s); if (a==31337) func(); } The aim is to execute a format string attack - to execute func() by inputting a string. I tried to use %n to overwrite the variable but I came to conclusion is that it is impossible without displaying b variable first and I have no idea how. Any hint would be appreciated. Sorry for my bad english. Let's try with and without printing: $ cat > f.c << \EOF #include <stdio.h> void func() { fprintf(stderr, "func\n"); } int main() { char s[200]; int a=123; int b=&a; #ifdef FIXER

Format string attack in printf

折月煮酒 提交于 2019-12-01 07:09:25
问题 #include <stdio.h> int main() { char s[200] int a=123; int b=&a; scanf("%50s",s); printf(s); if (a==31337) func(); } The aim is to execute a format string attack - to execute func() by inputting a string. I tried to use %n to overwrite the variable but I came to conclusion is that it is impossible without displaying b variable first and I have no idea how. Any hint would be appreciated. Sorry for my bad english. 回答1: Let's try with and without printing: $ cat > f.c << \EOF #include <stdio.h>

Format String Attack

故事扮演 提交于 2019-11-30 15:21:07
I have a small C program to be exploited. And I also understood the logic behind the attack to be performed. However, as much as I try, it is just not working for me. #include <stdio.h> #include <stdlib.h> #define SECRET1 0x44 #define SECRET2 0x55 int main(int argc, char *argv[]) { char user_input[100]; int *secret; int int_input; int a, b, c, d; /* other variables, not used here.*/ /* The secret value is stored on the heap */ secret = (int *) malloc(2*sizeof(int)); /* getting the secret */ secret[0] = SECRET1; secret[1] = SECRET2; printf("Please enter a decimal integer\n"); scanf("%d", &int

How to use Format String Attack

╄→гoц情女王★ 提交于 2019-11-30 13:28:13
问题 Assume I have the following code: #include <stdio.h> #include <stdlib.h> #include <fcntl.h> int num1 = 0; int main(int argc, char **argv){ double num2; int *ptr = &num1; printf(argv[1]); if (num1== 2527){ printf("Well done"); } if(num2 == 4.56) printf("You are a format string expert"); return 0; } I am trying to understand how to do it right but I just can't organize my mind with the guides on the internet. Is it suppose to something like: ./Program %p %p %p %p and then ./Program $( printf "

How can I use a percent % in FormatString without it multiplying by 100?

半城伤御伤魂 提交于 2019-11-30 06:26:25
问题 I would like to format an integer as a percent without it multiplying by 100 as shown here. Because my source is an int, dividing it first by 100 is not a valid option. Is this possible? [DisplayFormat(DataFormatString = "{0:#%}")] 回答1: You can escape the % character: [DisplayFormat(DataFormatString = @"{0:#\%}")] Note that there are two ways to use \ as an escape character: if you prefix a string literal with the verbatim symbol ( @ ), then \ characters are included in the string as-is,

Format String Attack

旧街凉风 提交于 2019-11-29 22:01:07
问题 I have a small C program to be exploited. And I also understood the logic behind the attack to be performed. However, as much as I try, it is just not working for me. #include <stdio.h> #include <stdlib.h> #define SECRET1 0x44 #define SECRET2 0x55 int main(int argc, char *argv[]) { char user_input[100]; int *secret; int int_input; int a, b, c, d; /* other variables, not used here.*/ /* The secret value is stored on the heap */ secret = (int *) malloc(2*sizeof(int)); /* getting the secret */