I have created the following C program to get data from an HTML form. But when I try to compile and run it, I get:
segmentation fault 11 (core dumped)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
char *N1,*N2,*N3,*N4,*N5;
int cgi_length;
char *cgi_data;
printf("Content-type:text/html\n\n");
cgi_length=atoi(getenv("CONTENT_LENGTH"));
cgi_data=malloc(cgi_length+1);
fread(cgi_data,1,cgi_length,stdin);
printf("<HTML>");
printf("<HEAD><TITLE>DATA</TITLE></HEAD><BODY>\n");
printf("<H3>DATA</H3>\n");
if(cgi_data == NULL)
{
printf("<P>Error! Error in passing data from form to script.");
}
else {
printf("%s",cgi_data);
sscanf(cgi_data,"N1=%s&N2=%s&N3=%S&N4=%SN5=%s",&N1,&N2,&N3,&N4,&N5);
printf("<P>N1 is %s and N2 is %s and N3 is %S and N4 is %S and N5 is %s.",N1,N2,N3,N4,N5);
}
}
Also if I use the ls command to see the data in the cgi-bin directory I see that a file named myprogram.cgi.core is created.
Does anyone know what is wrong?
I had the same problem before. I used fgets in a while loop. fread didn't work for me. Try this:
cgi_length=atoi(getenv("CONTENT_LENGTH"));
cgi_data=malloc(cgi_length+1);
while(fgets(cgi_data,cgi_length,stdin)!=NULL){
//insert some processing here
}
You have to check that the return value of your getenv function call is not a null pointer. Passing a null pointer to atoi is undefined behavior. I suggest also to check the return value of all your function calls and to use strtol function instead of atoi because atoi cannot detect errors.
Load the coredump in GDB. Try something like:
gdb myprogram.cgi myprogram.cgi.core
maybe replace myprogram.cgi with the proper name of the CGI.
When you are in GDB you could get a back trace to see where the application crashed by typing bt in the console.
Here you can find a quick tutorial on howto GDB: http://cs.baylor.edu/~donahoo/tools/gdb/tutorial.html
Few notes...:
Check what getenv returns, maybe it returns NULL instead of the value of the environment variable.
sscanf copies the values of the strings to the buffers where N1 N2 N3 N4 N5 are pointing to, it would be wise to allocate some memory for those pointers first...
You don't have to get the reference of the points N1 N2 N3 N4 N5
As Daniel Fischer noted: fread doesn't 0 terminate the string.
If you compile with -Wall (all warnings), it will give you an idea what's wrong
cgi.c: In function ‘main’:
cgi.c:23:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char **’ [-Wformat]
cgi.c:23:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘char **’ [-Wformat]
cgi.c:23:9: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 5 has type ‘char **’ [-Wformat]
cgi.c:23:9: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 6 has type ‘char **’ [-Wformat]
cgi.c:23:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 7 has type ‘char **’ [-Wformat]
cgi.c:24:9: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 4 has type ‘char *’ [-Wformat]
cgi.c:24:9: warning: format ‘%S’ expects argument of type ‘wchar_t *’, but argument 5 has type ‘char *’ [-Wformat]
cgi.c:26:1: warning: control reaches end of non-void function [-Wreturn-type]
There are several fundamental C programming errors. Use e.g. lint to find out some of them. For example, you’re declaring N1 as a character pointer but do not initialize it, and you pass its address as argument. You need to allocate a character array somehow and pass a pointer to it as an argument to sscanf.
I am pretty sure that you are getting zero in this line
cgi_length=atoi(getenv("CONTENT_LENGTH"));
Check if there is this environment variable actually defined. And always check for sizes before allocating memory and before putting some data into it.
Related
I want to add the mysql connection with the pcap loop which I am using in the code
MYSQL *con;
u_char *my_arguments = con;
pcap_loop(handle, total_packet_count, my_packet_handler, my_arguments);
but it is giving error
warning: initialization from incompatible pointer type
[-Wincompatible-pointer-types]
u_char *my_arguments = con;^~~
SO what should I do help is needed please
When I am directly putting value of con in pcap loop like pcap_loop(handle, total_packet_count, my_packet_handler, con); it is showing new error
Error is
passing argument 4 of ‘pcap_loop’ from incompatible pointer type
[-Wincompatible-pointer-types]
pcap_loop(handle, total_packet_count, my_packet_handler, con);
note: expected ‘u_char * {aka unsigned char *}’ but argument is of type ‘MYSQL * {aka struct st_mysql *}’
PCAP_API int pcap_loop(pcap_t *, int, pcap_handler, u_char *);
but I want it to be pushed in pcap loop
The "user" argument to pcap_loop(), pcap_dispatch(), and the callback probably should have been defined to be a void *, as it's a pointer to some arbitrary data that the callback understands, but it was defined to be a u_char * instead.
So you should cast the MYSQL * to u_char *:
pcap_loop(handle, total_packet_count, my_packet_handler, (u_char *)con);
When running clang-tidy on this minimal example posted below I get the (imho) false positive error from clang-tidy (full error trace at the end)
Value assigned to field 'id' in implicit constructor is garbage or undefined [clang-analyzer-core.uninitialized.Assign]
The code does nothing but assign a boost::variant to one of the two values. The error is gone if I replace the copy constructor by Size(const Size& sz) = default;. However, I cannot do this, since the Size is a cv::Size in the real code.
Even though it might not be as elegant as possible, I don't see any error with this. Can somebody point me into the direction if I am mistaken, clang-tidy or boost.
#include <boost/variant.hpp>
class Size
{
public:
Size() = default;
Size(const Size &sz) : width(sz.width) {}
int width{0};
};
struct B {
Size size;
};
struct A {
Size size;
uint32_t id{0};
};
int main() {
using T = boost::variant<A, B>;
T config = B();
}
I am running:
clang-tools-extra commit 50fe75789f08b96284d2c14cb6583b3783c74460
llvm commit afb8c1fed21eb4848d86f2d28e9cb3afcfbb2656
boost 1.67
Full error dump:
test_variant.cc:15:8: warning: Value assigned to field 'id' in implicit constructor is garbage or undefined [clang-analyzer-core.uninitialized.Assign]
struct A {
^
test_variant.cc:22:5: note: Calling move constructor for 'variant'
T config = B();
^
libraries/boost/include/boost/variant/variant.hpp:1880:9: note: Calling 'variant::internal_apply_visitor'
operand.internal_apply_visitor(visitor);
^
libraries/boost/include/boost/variant/variant.hpp:2466:16: note: Calling 'variant::internal_apply_visitor_impl'
return internal_apply_visitor_impl(
^
libraries/boost/include/boost/variant/variant.hpp:2452:16: note: Calling 'visitation_impl'
return detail::variant::visitation_impl(
^
libraries/boost/include/boost/variant/detail/visitation_impl.hpp:225:5: note: Control jumps to 'case 0:' at line 238
switch (logical_which)
^
libraries/boost/include/boost/variant/detail/visitation_impl.hpp:240:11: note: Calling 'visitation_impl_invoke'
, BOOST_VARIANT_AUX_APPLY_VISITOR_STEP_CASE
^
libraries/boost/include/boost/preprocessor/repetition/repeat.hpp:29:26: note: expanded from macro 'BOOST_PP_REPEAT'
# define BOOST_PP_REPEAT BOOST_PP_CAT(BOOST_PP_REPEAT_, BOOST_PP_AUTO_REC(BOOST_PP_REPEAT_P, 4))
^
libraries/boost/include/boost/preprocessor/cat.hpp:22:32: note: expanded from macro 'BOOST_PP_CAT'
# define BOOST_PP_CAT(a, b) BOOST_PP_CAT_I(a, b)
^
libraries/boost/include/boost/preprocessor/cat.hpp:29:34: note: expanded from macro 'BOOST_PP_CAT_I'
# define BOOST_PP_CAT_I(a, b) a ## b
^
note: (skipping 21 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
libraries/boost/include/boost/preprocessor/repetition/repeat.hpp:53:56: note: expanded from macro 'BOOST_PP_REPEAT_1_2'
# define BOOST_PP_REPEAT_1_2(m, d) BOOST_PP_REPEAT_1_1(m, d) m(2, 1, d)
^
libraries/boost/include/boost/preprocessor/repetition/repeat.hpp:52:36: note: expanded from macro 'BOOST_PP_REPEAT_1_1'
# define BOOST_PP_REPEAT_1_1(m, d) m(2, 0, d)
^
libraries/boost/include/boost/variant/detail/visitation_impl.hpp:231:16: note: expanded from macro 'BOOST_VARIANT_AUX_APPLY_VISITOR_STEP_CASE'
return (visitation_impl_invoke)( \
^
libraries/boost/include/boost/variant/detail/visitation_impl.hpp:154:12: note: Calling 'visitation_impl_invoke_impl'
return (visitation_impl_invoke_impl)(
^
libraries/boost/include/boost/variant/detail/visitation_impl.hpp:112:12: note: Calling 'move_into::internal_visit'
return visitor.internal_visit(
^
libraries/boost/include/boost/variant/variant.hpp:507:23: note: Calling implicit move constructor for 'A'
new(storage_) T(::boost::detail::variant::move(operand));
^
test_variant.cc:15:8: note: Value assigned to field 'id' in implicit constructor is garbage or undefined
struct A {
Ok, this is mostly about curiosity but I find it too strange.
Let's suppose I have this code
sig.mli
type t = A | B
main.ml
let f =
let open Sig in
function A | B -> ()
If I compile, everything will work.
Now, let's try to modify sig.mli
sig.mli
type t = A | B
exception Argh
and main.ml
main.ml
let f =
let open Sig in
function
| A -> ()
| B -> raise Argh
And let's try to compile it :
> ocamlc -o main sig.mli main.ml
File "main.ml", line 1:
Error: Error while linking main.cmo:
Reference to undefined global `Sig'
Well, is it just because I added the exception ? Maybe it means that exceptions are like functions or modules, you need a proper implementation.
But then, what if I write
main.ml
let f =
let open Sig in
function A | B -> ()
And try to compile ?
> ocamlc -o main sig.mli main.ml
>
It worked ! If I don't use the exception, it compiles !
There is no reason to this behaviour, right ? (I tested it on different compilers, 3.12.0, 4.00.0, 4.02.3 and 4.03.0 and all of them gave the same error)
Unlike variants, exception is not a pure type and requires its implementation in .ml file. Compile the following code with ocamlc -dlambda -c x.ml:
let x = Exit
-- the output --
(setglobal X!
(seq (opaque (global Pervasives!))
(let (x/1199 = (field 2 (global Pervasives!)))
(pseudo _none_(1)<ghost>:-1--1 (makeblock 0 x/1199)))))
You can see (let (x/1999 = (field 2 (global Pervasives!))).. which means assigning the value stored in the 2nd position of module Pervasives. This is the value of Exit. Exceptions have their values and therefore need .ml.
Variants do not require implementation. It is since their values can be constructed purely from their type information: constructors' tag integers. We cannot assign tag integers to exceptions (and their generalized version, open type constructors) since they are openly defined. Instead they define values for their identification in .ml.
To get an implementation of the exception, you need sig.ml. A .mli file is an interface file, a .ml file is an implementation file.
For this simple example you could just rename sig.mli to sig.ml:
$ cat sig.ml
type t = A | B
exception Argh
$ cat main.ml
let f =
let open Sig in
function
| A -> ()
| B -> raise Argh
$ ocamlc -o main sig.ml main.ml
I don't see a problem with this behavior, though it would be nice not to have to duplicate types and exceptions between .ml and .mli files. The current setup has the advantage of being simple and explicit. (I'm not a fan of compilers being too clever and doing things behind my back.)
How do you fix the following problem converting from decimal to binary?
void tobinary(int bin) {
string binary = Convert.ToInt32(bin, 2);
}
These are the errors:
Error 2: Argument 2: cannot convert from 'int' to 'System.IFormatProvider' 42
Error 1: The best overloaded method match for 'System.Convert.ToInt32(object, System.IFormatProvider)' has some invalid arguments 42
see:
Decimal to binary conversion in c #
it should be:
void tobinary(int bin) {
string binary = Convert.ToString(bin, 2);}
I am trying the haskell-json-service. When I run the code, it throws error here:
app req sendResponse = handle (sendResponse . invalidJson) $ do
value <- sourceRequestBody req $$ sinkParser json
newValue <- liftIO $ modValue value
sendResponse $ responseLBS
status200
[("Content-Type", "application/json")]
$ encode newValue
Error is,
Couldn't match type ‘conduit-1.2.4:Data.Conduit.Internal.Conduit.ConduitM
ByteString o0 m0 Value’
with ‘conduit-1.2.4.1:Data.Conduit.Internal.Conduit.ConduitM
ByteString Data.Void.Void IO Value’
NB: ‘conduit-1.2.4:Data.Conduit.Internal.Conduit.ConduitM’
is defined in ‘Data.Conduit.Internal.Conduit’
in package ‘conduit-1.2.4’
‘conduit-1.2.4.1:Data.Conduit.Internal.Conduit.ConduitM’
is defined in ‘Data.Conduit.Internal.Conduit’
in package ‘conduit-1.2.4.1’
Expected type: conduit-1.2.4.1:Data.Conduit.Internal.Conduit.Sink
ByteString IO Value
Actual type: conduit-1.2.4:Data.Conduit.Internal.Conduit.ConduitM
ByteString o0 m0 Value
In the second argument of ‘($$)’, namely ‘sinkParser json’
In a stmt of a 'do' block:
value <- sourceRequestBody req $$ sinkParser json
What does double dollar do? And what is this type - ByteString o0 m0 Value?
This appears to be the problem:
conduit-1.2.4:...
conduit-1.2.4.1:...
Your code is using a ByteString type from two different versions of the conduit library. From the point of view of GHC, these two types are unrelated: for instance, you can not pass the first type to a library function which expects the second one.
A cause for this could be using a library X which was compiled against the "old" conduit and a library Y which instead was compiled against the newer version. If your program imports X and Y, you will get in trouble when passing bytestrings from X to Y or vice versa. I have no idea about what X or Y actually are.
Maybe you can recompile X or Y so that they use the same version of conduit.