Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 153 Stack Exchange communities.

What is meta?
Here's how it works:
  1. Any Stack Exchange user can ask a question
  2. The community provides support, votes on ideas, and reports bugs
  3. Your voice helps shape the way Stack Exchange operates

I was wondering about the copyright of code snippets from answers. Let's say you ask a question and the solution consists of ten lines of code, which are given in an answer. Are there any restrictions about the use of that code?

It seems that the content of this page is under the creative commons license. So does one have to attribute the author if code is used in an open source project? Can it be legally used in closed source projects at all?

I guess ten lines of code are not really problematic, but is there a critical number of lines?

Return to FAQ index

share|improve this question
See my answer for a way to work around this. – waffles May 22 '09 at 7:53
possible duplicate of What is up with the source code license on Stack Overflow? – d3vid Sep 9 '14 at 13:59
@d3vid: this is the central FAQ; if there is any dupe closing it'll have to be in the other direction. – Martijn Pieters Sep 9 '14 at 14:35
Strictly talking about GPL software, see Sharing computer code between GPL software and CC-BY-SA communities. – jweyrich Sep 2 '15 at 17:46

13 Answers 13

up vote 34 down vote accepted

Anything that you post to Stack Overflow will be under the terms of the Creative Commons license. You can find more information by looking at the bottom right of the page and clicking the cc-wiki link.

As far as more specific legal issues, you should probably consult an attorney.

share|improve this answer
So basically if I paste a 10-line snippet of code from stackoverflow into my project, I must now distribute my project onder CC by-sa as well? – davr Sep 24 '08 at 17:54
If you include a code snippet from another source, you can't arbitrarily for that content to Creative Commons ... it stays under it's original license until the copyright holder says otherwise. – Steve Moyer Sep 24 '08 at 17:55
Nothing here changes who owned the code before it was posted. If the poster didn't have the right to post it (because of his company's IP provisions, or because it was taken from elsewhere) the CC license doesn't stop the original owner from claiming infringement against someone who downloads. – Will M Sep 24 '08 at 18:03
@Davr - If you plan on releasing the code under a license that would be incompatible with the creative commons license, I wouldn't post it to StackOverflow. I would imagine that this wouldn't be a problem for most FOSS licenses. And if this is proprietary code, I wouldn't post it to begin with. :) – Jason Baker Sep 24 '08 at 18:24
No...I'm talking about the other way around. I ask "How do I do Foo?", then someone answers with some source code, presumably licensed under CC by-sa. What happens when I take that source code, and add it to my non-opensource application? – davr Sep 24 '08 at 20:28
I modified a code I found on stackoverflow and add it to my program (which I intend to release under GPL). I mentioned in comments the person who wrote it and added the url to the topic on stackoverflow where I found it. Do I need to do anything else? – Adinan Nov 10 '13 at 15:43
@Jason: Realizing this wasn't Davr's question, I think your earlier comment missed something. IANAL, but afaict you can post a portion of closed source to SO and "dual license" (since it's all yours). If I have closed foo, sublicense foo to you, your posting foo to SO doesn't stop me from continuing to treat foo-superset as otherwise closed. Same if that "sublicensee" is myself. You can use foo on SO under CC, and I can use foo-superset as closed without suing myself. ;^) See MySql and dual licensing. – ruffin Nov 30 '13 at 17:12

This is a software licensing nightmare. If you paste in snippets in your answer that are in an incompatible license to cc-wiki, you are violating the terms of SO. I would imagine there are probably thousands of examples of such violations.

In addition to that major issue, I personally like to choose how to license my work. I am happy for all the text to be the viral cc-wiki. But am very unhappy to have my little code samples or classes under cc-wiki.

To combat this issue I just added this snippet to my profile on SO

All original source snippets I post on stackoverflow are dedicated to the public domain. Do with them as you see fit.

All those who are against the viral nature of the source samples should explicitly say what license their samples are in.

Another option is to post any larger snippets to sites like gist and have a license in the header of the file.

share|improve this answer
The "Public Domain" is not a license. Licenses are a type of contracts. The Public Domain is the set of works that fall outside of copyright, for whatever reason (eg too old, not creative, explicitly put there). Even so, your intent is clear enough to put your code in it. – MSalters May 22 '09 at 8:10
thanks, fixed it up – waffles May 22 '09 at 13:45
"Public domain" does not exist in many countries, so if you want everybody to be able to use your code snippets without worries, consider using CC0 (if you like Creative Commons licences), WTFPL, Unlicence, or the like. – Chris Jester-Young Nov 30 '11 at 21:08
This is much more helpful than the accepted answer, but for such a re-dedication to be meaningful, it really needs to be a license in itself (as @ChrisJester-Young noted). The license closest to the public domain is the Unlicense. – bukzor Mar 10 '14 at 18:25
Thanks! I've added this statement to my profile as well, including the Unlicense specifically. – Chris May 5 '14 at 20:37
@ChrisJester-Young, Is WTFPL a real copyright that actually works in court? – Pacerier Jun 17 '14 at 16:16
@Pacerier (I'm not a lawyer.) The only free software licence that I'm aware of having been tested in court is GPL. The only time WTFPL will get tested in court is if a copyright holder reneges on the licence and tries to sue "after the fact", since technically there is no way for the licensee to violate the licence. I have not heard of such a case. – Chris Jester-Young Jun 17 '14 at 17:03
@ChrisJester-Young, So shouldn't we instead opt for GPL rather than Unlicense and friends? – Pacerier Jun 17 '14 at 19:03
@Pacerier No, because here, our intent (as copyright holders) is to enable everyone to do anything they want with our code snippets. Using the GPL won't achieve that. CC0, Unlicence, and WTFPL are supposed to be "as close to public domain as possible" licences. The GPL is not such a licence. – Chris Jester-Young Jun 17 '14 at 19:05
@ChrisJester-Young, Actually, effectively wouldn't WTFPL and Unlicence actually allow other people to start claiming that they created our work? – Pacerier Jun 18 '14 at 18:55
@Pacerier nope. In many countries, attribution, integrity and a few other such rights can not be changed or given away or such. – chx Oct 12 '14 at 0:26
@chx, However no one knows who is speaking the truth. They can claim that it is theirs and there's no way to counter-prove that it belongs to me. – Pacerier Oct 12 '14 at 5:45
@waffles, I don't think your technique works because you are publishing on SO. For it to work you must first publish it at somewhere else under "public domain", then copy it into your SO post. Otherwise it wouldn't hold. – Pacerier Apr 12 '15 at 12:18

If you are talking about taking code from Stack Overflow for use in your work, I would be very careful. Even if it is the opinion of the people here that posts to Stack Overflow are covered by Creative Commons, you will need to be able to prove that you got the code from here.

A Short Story To Illustrate:

  • Someone else decides the code is a good solution for a problem they are facing, and decides to copy it into their open source GnuFizzBuzz project, covered by the GPL.
  • Five years from now, someone is doing an open source audit on your code (maybe you are selling to a nervous customer, or your company is going public).
  • The open source audit finds the snippet of code you got from Stack Overflow, and recognizes it as originating in GnuFizzBuzz.
  • You are now stuck explaining how/why you have GPL code mixed into your commercial product, or proving that the code was actually from Stack Overflow. Can you prove that the person posting the code to Stack Overflow owned it to begin with (maybe they actually copied it from GnuFizzBuzz to begin wih).

It may sound unlikely, but I have seen this exact situation with my own eyes. The safe thing to do is to regard any code snippets you find as explanations of how something could work. Read, understand, and write a solution based on your understanding. Don't ever cut/paste code you found on the Internet if you are working on a commercial product.

share|improve this answer
11, if including code from SO, include the URL in a comment. – msh210 Dec 19 '11 at 21:30
If you rely on specific code (and to which copyright applies), contact the author. CC-WIKI is not a software license and you want to have usage terms more clear than the CC-WIKI license is able to specify it. – hakre Jan 30 '12 at 15:05
In this answer (by Jeff Atwood actually) it's written that "a snippet of code falls under excerpt category and thus should be free to use under fair use" — but when is a snippet too long to be a snippet? – KajMagnus Feb 22 '13 at 11:00
@KajMagnus: That's a legal judgement, and it's wrong. Fair use doctrine does not apply to embedding excerpts of copyrighted work into source code. – bukzor Mar 10 '14 at 18:44

This has been discussed on UserVoice already. Jeff closed the request with this response:

I would hope that people are not posting giant blocks of code at SO, making it more of a quote / fair use type situation.

For commercial work, you should see your company lawyer before using any code from SO.

share|improve this answer
Thanks for the uservoice reference, seems that this is as clear as it gets. – nikow Sep 30 '08 at 15:36
Its clear, but as the owner of a copyright you have the right to license your work under whichever license you see fit. (in addition to the cc-wiki on this site) I attached a disclaimer to my profile saying that I am giving away my snippets. I hope others follow suite – waffles May 22 '09 at 7:54
@sambo99 That is a great idea. – Sinan Ünür May 22 '09 at 20:46
The only trouble is that there are legislations which unfortunately do not have a concept of "fair use". But I also agree that such small snippets will probably cause no problem, since all implementations of "standard" problems tend to look alike. I also want to hint to this CC FAQ about the use of CC licenses for software. – quazgar May 23 '13 at 11:26
Considering the Code Review site, where long/giant blocks of code is the norm, I'd say that Jeff's answer is quite outdated. – Simon Forsberg Dec 19 '13 at 1:45
@SimonAndréForsberg, Exactly, we even have Stack Snippets now which indirectly encourages longer code. – Pacerier Apr 12 '15 at 12:16

The two relevant questions are (1) what jurisdiction applies and (2) assuming US or sufficiently similar, is the snippet large and original enough to be covered under copyright law. Line counts are not magical; no amount of the following code would be covered:

switch(int i) {
  case 0: return "zero";
  case 1: return "one";
  case 2: return "two";

But if you can put that same functionality into 5 lines of code for i < 1<<32, those 5 lines will quite likely be considered sufficiently original and complex to be covered.

share|improve this answer

I am not a lawyer, but I think if you do a direct cut and paste of the code then you are going to have to look at what the site's cc-wiki gives you permission to do. Personally, I think that if you read the code, understand the code, and then use that knowledge to write you own take on what the code is doing you might skirt by the copyright issue as you aren't doing a cut and paste of the code.

However, it is worth noting that you do have to be aware that there might be a patent on the code in which case you have to be aware of those issues. That is an area were you are starting to look at having a lawyer render an option on it - if you work at a company then someone in the legal department might be able to give you an answer on the quesiton.

share|improve this answer

Only a lawyer can answer this question. But, very relevant are fair use and whether a small snippet of code is copyrightable.

Also, the implicit license it was posted under is relevant when asking a lawyer their opinionion. A lawyer might say that it would be difficult for someone to reasonably argue that their copyright was violated when code they post in response to a question is used in any way. Legal concepts such as promissory estoppel might come into play. It would also be interesting to see if reputation points could be deemed "consideration" under the law.

Patent issues are a much bigger risk, because the patent law is so nonsensical and keeps getting worse. Software patents are very dangerous and have already caused great damage to the software industry and free software.

Also, export restrictions (e.g. ITAR) are very dangerous, although some important precedents protecting source code as speech have been set. There was a time when simply having hooks to process data before sending it on its way was restricted as a munition in the US.

share|improve this answer
There is no implicit license. There's an explicit one - CC-BY-SA. – David Thornley Oct 15 '09 at 21:37

I've been careful to only use code snippets that do not reflect the core intellectual property of my company. I think there are definitely instances where I could divulge trade secrets in less than ten lines of code.

In any case, in instances where you're sharing code snippits from an open source project, it would be great to denote what license included code uses and maybe default to something generic like Creative Commons.

share|improve this answer

The cc licence link states that this page is attribution/share-alike.
That suggests (to me, IANAL) you CANNOT use a code sample in your commercial work.

The original poster of course retains copyright and so you could always ask them for permission.

share|improve this answer

In the US, the Copyright is owned by whomever posted the solution. That said, I really doubt anyone would post anything on this site with the intent of punishing people for using the code. If I didn't intend to make the code freely usable, I wouldn't post it, which is why I'm really careful about what I post and I try to make sure that it's been sanitized for anything I wouldn't want to give up.

share|improve this answer
It's owned by whoever owned it before it was posted - that may the poster, it may be the poster's employer or, for example, it may be the person or company that poster stole it from. – Will M Sep 24 '08 at 17:59

When someone posts code in here, he intends to give his code to everyone visiting the question. IANAL, but doesn't this automatically put the code into the public domain? (some form of duck-typing, in our terms)

share|improve this answer
While that technically is a matter for judges to decide, the Creative Commons reference on each page stronlgy suggests that it is not (yet) Public Domain. – MSalters May 22 '09 at 8:07
IANAL, but I don't think you can implicitly disclaim copyright. The legal system does recognize duck-typing for some things, but not everything. – David Thornley Oct 15 '09 at 21:36

I want to contradict most of the answers here (and try to explain why).

Before doing that, I'll say that I personally do attribute SO answers which suggest some code gizmo or idiom, but not because of licensing issues - out of appreciation and gratitude, and as a form of documentation of the voodoo I'll be typing in. I think that's the courteous thing to do.


No, you don't have to worry at all. Don't read anything like terms of use, definitions of licenses etc. - you should just not check whether these things exist on the website, and take it at face value: People answer your questions, or you theirs, and anyone can go ahead and use these answers and the code within them. (*)

It doesn't matter if you're a lawyer, better even if you aren't. You never really agreed to any of those legal constructs, and whoever wants to try to stick it to you because you used Slashdot code will need to bend over backwards and still won't be able to do it (especially if you're not in the US but not just). Another answer said it's a "licensing nightmare"? Not for you, for the lawyers who want to go after you. I won't say "really, trust me, I know this for certain"; I'm saying that this is the attitude I believe SE user should adopt, and I think SO has no chance whatsoever to enforce what they consider to be the terms of use, and that they won't try to, and that answer-posters also have no chance to get to you, and that they won't try either. So don't worry.

Quoting Richard Stallman here:

We'll kick out those dirty licenses
Ever more, hackers, ever more.

(*) Of course if you intend to use SE material to pursue your own legalistic IP agenda, e.g. registering patents or whatever - then you're digging your own grave my friend. Also I hope you didn't ask a SO question such as "Can u post teh code of teh Microsoft Windows kernel plz".

share|improve this answer

You don't have to worry - why would you be worried, why would you release stress hormones to the the organism?

There is a cure - open-sourcing your code under Creative Commons licence: enter image description here

Today I wrote a post about it:

  • My feeling: CC by-sa 3.0 was introduced to reduce legal burden.
  • My feeling: There are other, more applicable licenses to code.
  • My feeling: Dual-licensing for various bits & bobs (maybe?)

(I wonder how my point of view will evolve in 5 years)

share|improve this answer

You must log in to answer this question.

protected by Robert Harvey Aug 21 '13 at 2:20

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged .